Friday 23 January 2015

C# Program to find the largest among three numbers - Using If Else

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter the a,b and c value");
            int a, b, c;
            a = Convert.ToInt32(Console.ReadLine());
            //or
            // a = int.Parse(Console.ReadLine());
            b = Convert.ToInt32(Console.ReadLine());
            c = Convert.ToInt32(Console.ReadLine());
            if (a > b)
            {
                if (a == c)
                {
                Console.WriteLine("A and c are greatest and are equal");
                }
                else if (a > c)
                {
                    Console.WriteLine("A is bigger: {0}", a);
                }
                else
                {
                    Console.WriteLine("C is bigger: {0}", c);
                }

            }
            //
            else if (b > c)
            {
                if (b == a)
                {
                    Console.WriteLine("A and B is greater and Equal");
                }
                else if (b > a)
                {
                    Console.WriteLine("B is bigger: {0}", b);
                }
                else
                {
                    Console.WriteLine("A is bigger: {0}", a);
                }

            }
            //
            else if (a > c)
            {

                if (a > b)
                {
                    Console.WriteLine("A is bigger: {0}", a);
                }
                else
                {
                    Console.WriteLine("B is bigger: {0}", b);
                }                             
            }
             else if (c > a)
            {
                {
                 Console.WriteLine("c is bigger: {0}", c);
                }
               
             }
            else if (b > a)
            {
                {
                  Console.WriteLine("B is bigger: {0}", b);
                }

            }
           
              
                else if (b == c)
                {
                    if (b == a)
                    {
          Console.WriteLine("A , B , C is equal the value is:{0}", a);
                    }
                    else
                    {
          Console.WriteLine("B , C  is greatest and are equal ");
                    }

                }
                else if (c == a)
                {
                    if (c == b)
                    {
          Console.WriteLine("A , B , C is equal the value is:{0}", a);
                    }
                    else
                    {
         Console.WriteLine("A , C  is equal: {0}",c);
                    }

                }                              
         Console.ReadLine();
        }
    }

    }




C# Understanding Write and WriteLine()


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WriteandWriteLine
{
    class Program
    {
        static void Main(string[] args)
        { 
            Console.Write("ONE"); // THIS PRINTS ONE
            Console.Write(" TWO"); // THIS PRINTS TWO IN SAME LINE.
            Console.WriteLine(" THREE");// THIS WOULD HAVE PRINTED IN NEWLINE IF WE COMMENT OUT TOP TWO WRITE.
            Console.WriteLine("FOUR"); // THIS PRINTS IN NEWLINE
            Console.WriteLine("FIVE");  // THIS PRINTS IN NEWLINE
            Console.ReadLine(); // Console.ReadLine(); is to keep focus the window
        }
    }
}



C# Basic Program - Understanding For Loop


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace For_five
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("--- For 1 ---");
            for (int i = 0; i <= 10; i++)
            {
                Console.WriteLine(i);
            }

             Console.WriteLine("--- For 2 ---");
            for (int i = 10 - 1; i >= 0; i--)
            {
                Console.WriteLine(i);
            }

            Console.WriteLine();

            Console.WriteLine("--- For 3 ---");
            for (int i = 0; i < 10; i += 2)
            {
                Console.WriteLine(i);
            }

            Console.WriteLine();

            Console.WriteLine("--- For 4 ---");
            for (int i = 10 - 1; i >= 0; i -= 2)
            {
                Console.WriteLine(i);
            }

            Console.WriteLine();

            Console.WriteLine("--- For 5 ---");
            for (int i = 0; i < (20 / 2); i += 2)
            {
                Console.WriteLine(i);
            }

            Console.ReadLine();
        }
        
    }
}



C# Program Interview Programs - Triangle and Right angled triangle


using System;
using System.Collections.Generic;
using System.Text;

namespace example1
{
    class Program
    {
        static void Main(string[] args)
        // to print right angled triangle
        {
            int a = 5;

            for (int i = 1; i <= 5; i++)
            {
                for (int j = 1; j <= i; j++)
                {
                    Console.Write(" *");
                }
                Console.WriteLine();
            }
            Console.ReadKey();


    // to print triangle or a tree shape.

            for (int i = 1; i <= 6; i++)
            {
                for (int k = 1; k <= 6 - i; k++)
                {
                    Console.Write(" ");
                }
                for (int k = 1; k <= i; k++)
                {
                    Console.Write(" *");
                }
                Console.WriteLine();
            }
  
            Console.ReadLine();
        }
    }
}



C# Example program for do while

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace @while
{
    class Program
    {
        static void Main(string[] args)            
        {
            int i = 1;
            while (i < 5)
             //while (i++ < 8)
           //  while (++i < 5)
           //  while (i-- < 5)
          //  while (--i < 5)
            {
                Console.WriteLine(i);
                i++;

            }
            Console.ReadLine();
        }
    }
}



C# Example program for dowhile

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace do_while
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = 0;
            do
            {
                Console.WriteLine(i);
                i++;
            }
            while (i < 5);
            Console.ReadLine();
        }
       
    }
}


C # Basic Programs to print a line using various methods

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Csharptestapplication
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Box");
            Console.Write("Table");
            Console.Write("chair");
            Console.WriteLine();
            Console.WriteLine("desk");
          //  Console.ReadLine();
            //Console.ReadKey();
           // Console.Read();
        }
    }
}

-----------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Test3
{
    class Program
    {
        static void Main(string[] args)
        {
            Program.test();
        }

        public static void test()
        {
            Console.WriteLine("hi World");
        }
    }
}

-----------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Test4
{
    class Program
    {
        static void Main(string[] args)
        {
            a ob = new a();
            ob.test();
        }
    }

    class a
    {
        public void test()
        {
            Console.WriteLine("hi World");
        }
    }
}

-----------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Test5
{
    class Program
    {
        static void Main(string[] args)
        {
            a.test();
        }
    }

    class a
    {
        public static void test()
        {
            Console.WriteLine("hi World");
        }
    }

}

-----------------------------------------------------------------------------------------


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Test6
{
    class Program
    {
        static void Main(string[] args)
        {
            a ob1 = new a();
            ob1.test1();

            b ob2 = new b();
            ob2.test2();

        }
    }

    class a
    {
        public void test1()
        {
            Console.WriteLine("hi World 1");
        }
    }

    class b
    {
        public void test2()
        {
            Console.WriteLine("hi World 2");
        }
    }

}

-----------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Test7
{
    class Program
    {
        static void Main(string[] args)
        {
            a ob = new a();
            ob.test1();
            ob.test2();
          
        }
    }

    class a
    {
        public void test1()
        {
            Console.WriteLine("hi World 1");
        }

        public void test2()
        {
            Console.WriteLine("hi World 2");
        }
    }
}
-----------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Test8
{
    class Program
    {
        static void Main(string[] args)
        {
            b ob = new b();
            ob.test1();
            b.test2();
        }
    }

    class a
    {
        public void test1()
        {
            Console.WriteLine("hi world 1");
        }
    }

    class b:a
    {
        public static void test2()
        {
            Console.WriteLine("hi world 2");
        }
    }
}

-----------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Test9
{
    class Program:a
    {
        static void Main(string[] args)
        {
            Program ob = new Program();
            ob.test1();
            ob.test2();
        }

        public void test2()
        {
            Console.WriteLine("hi world 1");
        }
    }

    class a
    {
        public void test1()
        {
            Console.WriteLine("hi world 2");
        }
    }
}