Showing posts with label consols. Show all posts
Showing posts with label consols. Show all posts

31 July 2011

Reverse of a string or number in console apps


class Reverseofno
    {
        public static void Main()
        {
            Console.WriteLine("enter a no/string: ");
            string a = Console.ReadLine();
            for (int x = a.Length - 1; x >= 0; x--)
            {
                Console.Write(a[x]);
            }

            Console.WriteLine();
           
            string bb = string.Empty;
            Console.WriteLine("enter a no/string: ");
            string b = Console.ReadLine();
            for (int x = b.Length - 1; x >= 0; x--)
            {
                bb += b[x].ToString();
            }
            string[] bbb = new string[bb.Split(' ').Length];
            for (int y = 0; y <= bb.Split(' ').Length-1; y++)
            {
                bbb[y] = bb.Split(' ')[y]+" ";
            }
            for (int z = bbb.Length - 1; z >= 0; z--)
            {
                Console.Write(bbb[z]);
            }
           
            Console.WriteLine();
           
            Console.WriteLine("enter a name: ");
            string n= Console.ReadLine();
            if(bbb.Contains(n+" "))
                Console.WriteLine("found");
            else
                Console.WriteLine("not found");
            Console.ReadLine();
        }
    }

Interface overriding in console apps


interface Interface
{
      void add(int x, int y);
}
class overrideinterface : Interface
{
      public void add(int a, int b)
      {
            Console.WriteLine("add is: " + (a + b));
      }
      public static void Main()
      {
            overrideinterface ovri = new overrideinterface();
            ovri.add(5, 5);
            Console.ReadLine();
      }
}

Note: A class can inherit any no.of interfaces separated by comma(,)

Virtual class overriding


class Virtual
{
        public virtual void add(int a, int b)
        {
            Console.WriteLine(a + b);
        }
}
class overridevirtual:Virtual
{
        public override void add(int a, int b)
        {
            Console.WriteLine(a);
        }
        public static void Main()
        {
            Virtual vir = new Virtual();
            vir.add(1,2);
            Console.ReadLine();
        }
}

Abstract Overriding in console apps


abstract class Abstract
{
      public abstract void mul(int x);
      public void add(int x, int y)
      {
          Console.WriteLine("add is: "+ (x + y));
      }
}
class overrideabstract : Abstract
{
      public override void mul(int a)
      {
          Console.WriteLine("mul is: "+ a*a);
      }
      public static void Main()
      {
          overrideabstract ovra = new overrideabstract();
          ovra.add(4, 5);
          ovra.mul(3);
          Console.ReadLine();
      }
}

Prime No. in console apps


class Prime
    {
        static void Main(string[] args)
        {
            int j = 0;
            int count = 0;
            Console.WriteLine("Plz enter a number: ");
            int n =int.Parse( Console.ReadLine());

            Console.WriteLine("The Prime no's are:");
            for (int i = 2; i <= n; i++)
            {
                for (j = 2; j <= i; j++)
                {
                    if (i % j == 0)
                    {
                        break;
                    }
                }
                if (i == j)
                {
                    Console.WriteLine(i);
                    count++;
                }
            }
            Console.WriteLine();
            Console.Write("Total Prime No's: "+count);
            Console.ReadLine();
        }

Constructor OverLoading in Console apps

class CACOverLoading
{
        int x;
        public CACOverLoading()
        {
            Console.WriteLine("Default constructor");
            x = 123;
        }
        public CACOverLoading(int x)
        {
            Console.WriteLine("Parameterized constructor");
            Console.WriteLine(x);
            this.x = 789;
        }
        public void display()
        {
            Console.WriteLine(x);
        }
        public static void Main()
        {
            CACOverLoading col = new CACOverLoading();
            col.display();
            CACOverLoading col1 = new CACOverLoading(456);
            col1.display();
            Console.ReadLine();
        }
}

Method OverLoading in console apps


class mOverLoading
    {
        int x=10;
        public void show()
        {
            Console.WriteLine("default constructor");
            Console.WriteLine(x + this.x);
        }
        public void show(int x)
        {
            Console.WriteLine("Parameterized constructors");
            Console.WriteLine(x+" "+this.x);
        }
        public void show(string s, int x)
        {
            Console.WriteLine("Parameterized constructor");
            Console.WriteLine(s + " " + x);
        }
        public static void Main()
        {
            mOverLoading mol = new mOverLoading();
            mol.show();
            mol.show(123);
            mol.show("satya", 456);
            Console.ReadLine();
        }
    }

Operator OverLoading in console apps


class ClsComplex
{
        int R, I;
        public ClsComplex(int R, int I)
        {
            this.R = R;
            this.I = I;
            Console.WriteLine("{0} + {1}i", R, I);
        }
        public static ClsComplex operator +(ClsComplex Num1, ClsComplex Num2)
        {
            ClsComplex Num3 = new ClsComplex(Num1.R + Num2.R, Num1.I + Num2.I);
            return Num3;
        }
}
class ClsOpOverLoad
{
        static void Main(string[] args)
        {
            Console.Write("C1= ");
            ClsComplex C1 = new ClsComplex(4, 5);
            Console.Write("C2= ");
            ClsComplex C2 = new ClsComplex(8, 2);
            Console.Write("C3= ");
            ClsComplex C3 = C1 + C2;
            Console.Read();
        }
}

30 July 2011

Reverse of a string or number in console apps


class Reverseofno
    {
        public static void Main()
        {
            Console.WriteLine("enter a no/string: ");
            string a = Console.ReadLine();
            for (int x = a.Length - 1; x >= 0; x--)
            {
                Console.Write(a[x]);
            }

            Console.WriteLine();
           
            string bb = string.Empty;
            Console.WriteLine("enter a no/string: ");
            string b = Console.ReadLine();
            for (int x = b.Length - 1; x >= 0; x--)
            {
                bb += b[x].ToString();
            }
            string[] bbb = new string[bb.Split(' ').Length];
            for (int y = 0; y <= bb.Split(' ').Length-1; y++)
            {
                bbb[y] = bb.Split(' ')[y]+" ";
            }
            for (int z = bbb.Length - 1; z >= 0; z--)
            {
                Console.Write(bbb[z]);
            }
           
            Console.WriteLine();
           
            Console.WriteLine("enter a name: ");
            string n= Console.ReadLine();
            if(bbb.Contains(n+" "))
                Console.WriteLine("found");
            else
                Console.WriteLine("not found");
            Console.ReadLine();
        }
    }

Interface overriding in console apps


interface Interface
    {
        void add(int x, int y);
    }
    class overrideinterface : Interface
    {
        public void add(int a, int b)
        {
            Console.WriteLine("add is: " + (a + b));
        }
        public static void Main()
        {
            overrideinterface ovri = new overrideinterface();
            ovri.add(5, 5);
            Console.ReadLine();
        }
    }

Abstract Overriding in console apps


abstract class Abstract
    {
        public abstract void mul(int x);
        public void add(int x, int y)
        {
            Console.WriteLine("add is: "+(x + y));
        }
    }
    class overrideabstract : Abstract
    {
        public override void mul(int a)
        {
            Console.WriteLine("mul is: "+a*a);
        }
        public static void Main()
        {
            overrideabstract ovra = new overrideabstract();
            ovra.add(4, 5);
            ovra.mul(3);
            Console.ReadLine();
        }
    }

Virtual class overriding


class Virtual
    {
        public virtual void add(int a, int b)
        {
            Console.WriteLine(a + b);
        }
    }
    class overridevirtual:Virtual
    {
        public override void add(int a, int b)
        {
            Console.WriteLine(a);
        }
        public static void Main()
        {
            Virtual vir = new Virtual();
            vir.add(1,2);
            Console.ReadLine();
        }
    }

Prime No. in console apps


class Prime
    {
        static void Main(string[] args)
        {
            int j = 0;
            int count = 0;
            Console.WriteLine("Plz enter a number: ");
            int n =int.Parse( Console.ReadLine());

            Console.WriteLine("The Prime no's are:");
            for (int i = 2; i <= n; i++)
            {
                for (j = 2; j <= i; j++)
                {
                    if (i % j == 0)
                    {
                        break;
                    }
                }
                if (i == j)
                {
                    Console.WriteLine(i);
                    count++;
                }
            }
            Console.WriteLine();
            Console.Write("Total Prime No's: "+count);
            Console.ReadLine();
        }

Constructor OverLoading in Console apps


class CACOverLoading
    {
        int x;
        public CACOverLoading()
        {
            Console.WriteLine("Default constructor");
            x = 123;
        }
        public CACOverLoading(int x)
        {
            Console.WriteLine("Parameterized constructor");
            Console.WriteLine(x);
            this.x = 789;
        }
        public void display()
        {
            Console.WriteLine(x);
        }
        public static void Main()
        {
            CACOverLoading col = new CACOverLoading();
            col.display();
            CACOverLoading col1 = new CACOverLoading(456);
            col1.display();
            Console.ReadLine();
        }
    }

Method OverLoading in console apps


class mOverLoading
    {
        int x=10;
        public void show()
        {
            Console.WriteLine("default constructor");
            Console.WriteLine(x + this.x);
        }
        public void show(int x)
        {
            Console.WriteLine("Parameterized constructors");
            Console.WriteLine(x+" "+this.x);
        }
        public void show(string s, int x)
        {
            Console.WriteLine("Parameterized constructor");
            Console.WriteLine(s + " " + x);
        }
        public static void Main()
        {
            mOverLoading mol = new mOverLoading();
            mol.show();
            mol.show(123);
            mol.show("satya", 456);
            Console.ReadLine();
        }
    }

Operator OverLoading in console apps


class ClsComplex
    {
        int R, I;
        public ClsComplex(int R, int I)
        {
            this.R = R;
            this.I = I;
            Console.WriteLine("{0} + {1}i", R, I);
        }
        public static ClsComplex operator +(ClsComplex Num1, ClsComplex Num2)
        {
            ClsComplex Num3 = new ClsComplex(Num1.R + Num2.R, Num1.I + Num2.I);
            return Num3;
        }
    }
    class ClsOpOverLoad
    {
        static void Main(string[] args)
        {
            Console.Write("C1= ");
            ClsComplex C1 = new ClsComplex(4, 5);
            Console.Write("C2= ");
            ClsComplex C2 = new ClsComplex(8, 2);
            Console.Write("C3= ");
            ClsComplex C3 = C1 + C2;
            Console.Read();
        }
    }

26 July 2011

palindrome in console applications


string str = string.Empty;
            Console.WriteLine("Enter a String");
            string s = Console.ReadLine();
            int i = s.Length;
            for (int j = i - 1; j >= 0; j--)
            {
                str = str + s[j];
            }
            if (str == s)
            {
                Console.WriteLine(s + " is palindrome");
            }
            else
            {
                Console.WriteLine(s + " is not a palindrome");
            }

factorial of number in console applications


            int count = 1;
            Console.Write("Enter Number for factorial: ");
            int number = Convert.ToInt32(Console.ReadLine());
            for (int i = 1; i <= number; i++)
            {
                count = count * i;
            }
            Console.Write(count);
            Console.Read();

or
          public static long numfact(long n)
          {
           if (n <= 1)
               return 1;
           else
             return n * numfact(n - 1);
           }
           Console.Write(numfact(5));

palindrome in console applications


string str = string.Empty;
            Console.WriteLine("Enter a String");
            string s = Console.ReadLine();
            int i = s.Length;
            for (int j = i - 1; j >= 0; j--)
            {
                str = str + s[j];
            }
            if (str == s)
            {
                Console.WriteLine(s + " is palindrome");
            }
            else
            {
                Console.WriteLine(s + " is not a palindrome");
            }

factorial of number in console applications


            int count = 1;
            Console.Write("Enter Number for factorial: ");
            int number = Convert.ToInt32(Console.ReadLine());
            for (int i = 1; i <= number; i++)
            {
                count = count * i;
            }
            Console.Write(count);
            Console.Read();

or
          public static long numfact(long n)
          {
           if (n <= 1)
               return 1;
           else
             return n * numfact(n - 1);
           }
           Console.Write(numfact(5));