Saturday, 29 December 2012

Basic Concepts Programming Code

Arrays Example:


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

namespace ArraysEg
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] A;
            A = new int[10];

            A[0] = 67;
            A[1] = A[2] = A[3] = 56;
            A[4] = A[1] + A[2] + A[3];
            A[5] = A[6] = A[7] = A[4] - 10;
            A[8] = A[9] = int.Parse(Console.ReadLine());


            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine(A[i]);

            }
            Console.ReadLine();

        }
    }
}


DoWhileLoop:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
    
namespace DoWhileEg
{
    class Program
    {
        static void Main(string[] args)
        {
            int n;
            do{
                n=int.Parse(Console.ReadLine());
            }
            while(n!=0);

            Console.WriteLine("You Have Entered " + n);
        }
    }
}



 ElseIfLadderExample:

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

namespace ElseIfLadderEg
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 934;
            int b = 78;
            int c = 67;
            int d = 678;

            if (a > b && a > c && a>d)
                Console.WriteLine("Greatest is " + a);
            
            else
            {
                if (b > a && b > c && b > d)
                    Console.WriteLine("Greatest is " + b);
                else
                {
                    if (c > a && c > b && c > d)
                        Console.WriteLine("Greatest is " + c);
                    else
                        Console.WriteLine("Greatest is " + d);
                }
            }


            if (a > b && a > c && a > d)
                Console.WriteLine("Greatest is " + a);
            else if (b > a && b > c && b > d)
                Console.WriteLine("Greatest is " + b);
            else if (c > a && c > b && c > d)
                Console.WriteLine("Greatest is " + c);
            else
                Console.WriteLine("Greatest is " + d);



            int s1, s2, s3;
            double Avg;
            s1 = 56;
            s2 = 67;
            s3 = 86;
            Avg = (s1+s2+s3) / 3.0;
            Console.WriteLine("Avg: " + Avg);

            if (Avg < 35)
                Console.WriteLine("Fail");

            else if (Avg >= 35 && Avg < 50)
                Console.WriteLine("3rd");

            else if (Avg >= 50 && Avg < 60)
                Console.WriteLine("2nd");


            else if (Avg >= 60 && Avg < 70)
                Console.WriteLine("1st");

            else if (Avg >= 70)
                Console.WriteLine("Dist");


            Console.ReadLine();
        }
    }
}



For Loop Example:

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

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

            for (int i = 1; i <=10; i++)
            {
                Console.WriteLine(i);
            }

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

            for (int i = 1; i <= 100; i++)
            {
                if (i % 2 == 0)
                    Console.WriteLine(i);
            }

            int n = 8;

            for (int i = 1; i <= 10; i++)
            {
                Console.WriteLine("{0} * {1} = {2}", n, i, n * i);
            }

            Console.ReadLine();
        }
    }
}


IF Condition Example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace IfEg
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 78;
            int b = 145;

            if (a > b)
                Console.WriteLine("Greater is " + a);

            else
                Console.WriteLine("Greater is " + b);

            Console.ReadLine();
        }
    }
}

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

namespace MathOperationsEg
{
    class Program
    {
        static void Main(string[] args)
        {
            int n1, n2, n3;
            Console.WriteLine("Enter the value for n1");
            n1 =int.Parse(Console.ReadLine());

            Console.WriteLine("Enter the value for n2");
            n2 =int.Parse( Console.ReadLine());

            n3 = n1 * n2;
            Console.WriteLine("Sum of {0} and {1} is {2}",n1,n2,n3);
            Console.WriteLine("Sum of " + n1 + " and " + n2 + " is " + n3);

            Console.ReadLine();
        }
    }
}


StructEg:

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

namespace StructEg
{
    struct Student
    {
       public int Sid;
       public string SName;
       public double SAvg;
    }
    class Program
    {
        static void Main(string[] args)
        {

            //Student s;

            //s.Sid = 1221;
            //s.SName = "Manzoor";
            //s.SAvg = 88.9;


            //Console.WriteLine("Sid: " + s.Sid);
            //Console.WriteLine("SName: " + s.SName);
            //Console.WriteLine("SAvg: " + s.SAvg);



            Student[] S = new Student[3];

            for (int i = 0; i < S.Length; i++)
            {
                Console.WriteLine("Enter Sid");
                S[i].Sid = int.Parse(Console.ReadLine());

                Console.WriteLine("Enter SName");
                S[i].SName = Console.ReadLine();

                Console.WriteLine("Enter Avg");
                S[i].SAvg = double.Parse(Console.ReadLine());
            }



            Console.WriteLine("Student(s) Who Secured More Than 80%");
            foreach (Student k in S)
            {
                if (k.SAvg >= 80)
                {
                    Console.WriteLine("Record");
                    Console.WriteLine("Sid: " + k.Sid);
                    Console.WriteLine("SName: " + k.SName);
                    Console.WriteLine("SAvg: " + k.SAvg);
                }
            }


            //for (int i = 0; i < S.Length; i++)
            //{
            //    Console.WriteLine("Record");
            //    Console.WriteLine("Sid: " + S[i].Sid);
            //    Console.WriteLine("SName: " + S[i].SName);
            //    Console.WriteLine("SAvg: " + S[i].SAvg);
            //}

            Console.ReadLine();

        }
    }
}



SwitchCaseEg:

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

namespace SwitchCaseEg
{
    class Program
    {
        static void Main(string[] args)
        {
            int a=24 ;
            int b=52 ;

        
            Console.WriteLine("+.Add -.Sub *.Mul /.Div");
            string ch =Console.ReadLine();

            switch (ch)
            {
                case "*":
                    Console.WriteLine(a * b);
                    break;

                case "/":
                    Console.WriteLine((double)a / b);
                    break;
                case "+":
                    Console.WriteLine(a + b);
                    break;

                case "-":
                    Console.WriteLine(a - b);
                    break;
                                   

                default:
                    Console.WriteLine("Invalid Input");
                    break;
            }
            
            Console.ReadLine();
        }
    }
}



TypeCasting Example:

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

namespace TypeCastEg
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 5;
            int b = 2;

            Console.WriteLine((double)a/b);
            Console.ReadLine();
        }
    }
}



While Loop:

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

namespace WhileEg
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = 0;
            while (i < 10)
            {
                Console.WriteLine("ManzoorTheTrainer");
                i++;
            }

            int n = int.Parse(Console.ReadLine());
            int sum = 0;
            int r;
            while (n != 0)
            {
                r = n % 10;
                sum = sum + r;
                n = n / 10;
            }

            Console.WriteLine("Sum is " + sum);

            Console.ReadLine();
        }
    }
}



Class Examle:

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

namespace ClassEg1
{
    class Customer
    {
        int AcNo;
        string Name;
        double Bal;

        public Customer()
        {
            Console.WriteLine("Hello World");
        }

        public Customer(int a, string n, double b)
        {
            AcNo = a;
            Name = n;
            Bal = b;
        }

        public void Deposit(double amt)
        {
            Bal = Bal + amt;
        }

        public void WithDraw(double amt)
        {
            Bal = Bal - amt;
        }

        //public void CreateCustomer(int a, string n, double b)
        //{
        //    AcNo = a;
        //    Name = n;
        //    Bal = b;
        //}

        public void BalEnq()
        {
            Console.WriteLine("AcNO:{0} Name:{1} Bal:{2}",AcNo,Name,Bal);
        }

        public void BalEnq(double amt, string f)
        {
            if (f == "D")
            {
                //Bal = Bal + amt;
                Bal += amt;
            }
            else if (f == "W")
            {
                Bal -= amt;
            }
            else
            {
                Console.WriteLine("INVALID Flag");
            }

            Console.WriteLine("AcNO:{0} Name:{1} Bal:{2}", AcNo, Name, Bal);
        }

    }
    class Program
    {
        static void Main(string[] args)
        {
            Customer c = new Customer(1234, "Jack", 78000);
            
            //c.CreateCustomer(1234, "Jack", 78000);

            //c.BalEnq();

            //c.CreateCustomer(1234, "Jack", 80000);

            c.BalEnq();

            c.Deposit(3000);

            c.BalEnq();

            c.WithDraw(5000);

            c.BalEnq();

            c.BalEnq(4500, "D");

            Console.ReadLine();
        }
    }
}



NameSpaceEg:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TeamB.GroupA;


//namespace TeamA
//{
//    class Student
//    {
//        public void Display()
//        {
//        }
//    }
//}

namespace TeamB
{

    class Student
    {
        public void Show()
        {
        }
    }

    class Teacher
    {
        public void GetSalary()
        {
        }
    }

    namespace GroupA
    {
        class Test
        {
            public void Go()
            {
               System.Console.WriteLine("This is a Go method of class Test which is in Group A namespace of TeamB Namespace");
            }
        }
    }
}

namespace NameSpaceEg
{
    class Program
    {
        static void Main(string[] args)
        {
            //TeamA.Student S = new TeamA.Student();
            //TeamB.Student S1 = new TeamB.Student();
            //S.Display();
            //S1.Show();

            //TeamB.GroupA.Test t = new TeamB.GroupA.Test();

            Test t = new Test();

            t.Go();
            System.Console.ReadLine();
        }
    }
}




Products Table Eg:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Products
{
    public class Product
    {
        public double GetBillAmount(int PricePerUnit, int NoOfUnits)
        {
            double amt;
            amt = PricePerUnit * NoOfUnits;
            return amt;
        }
    }
}



Properties Example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PropertiesEg
{
    class Student
    {
        int Sid;
        string SName;
        double m1, m2, m3;

        public int SID
        {
            get { return Sid; }
        }

        public double M1
        {
            set { m1 = value; }
            get { return m1; }
        }

        public double M2
        {
            set { m2 = value; }
            get { return m2; }
        }

        public double M3
        {
            set { m3 = value; }
            get { return m3; }
        }

        public Student(int Sid, string SName)
        {
            this.Sid = Sid;
            this.SName = SName;
        }

        public void SetMarks(int m1,int m2,int m3)
        {
            this.m1 = m1;
            this.m2 = m2;
            this.m3 = m3;
        }

        //public double Getm1()
        //{
        //    return m1;
        //}

        //public double Getm2()
        //{
        //    return m2;
        //}

        //public double Getm3()
        //{
        //    return m3;
        //}

        //public void Setm1(double m1)
        //{
        //    this.m1 = m1;
        //}
        //public void Setm2(double m2)
        //{
        //    this.m2 = m2;
        //}
        //public void Setm3(double m3)
        //{
        //    this.m3 = m3;
        //}

        public void DisplayAvg()
        {
            Console.WriteLine("Sid:{0} SName:{1} m1:{2} m2:{3} m3:{4} Avg:{5}",Sid,SName,m1,m2,m3,(m1+m2+m3)/3.0);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Student S = new Student(123, "Peter");
            S.DisplayAvg();

            Console.WriteLine("Student Id:" + S.SID);

            S.M1 = 67;
            S.M2 = 78;
            S.M3 = 58;

            Console.WriteLine("M1:" + S.M1);
            Console.WriteLine("M2:" + S.M2);
            Console.WriteLine("M3:" + S.M3);

            S.DisplayAvg();

            //S.SetMarks(34, 56, 78);
            //S.DisplayAvg();

            //Console.WriteLine("M1:" + S.Getm1());
            //Console.WriteLine("M2:" + S.Getm2());
            //Console.WriteLine("M3:" + S.Getm3());


            //S.Setm1(67);

            //S.DisplayAvg();

            Console.ReadLine();
        }
    }
}





Static Example:

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

namespace StaticEg
{
    class HousingLoan
    {
        int AcNo;
        string Name;
        double LoanAmount;
        static double ROI;

        static HousingLoan()
        {
            ROI = 12.8;
        }

        public HousingLoan(int AcNo,
        string Name,
        double LoanAmount)
        {
            this.AcNo = AcNo;
            this.Name = Name;
            this.LoanAmount = LoanAmount;
        }

        public void Display()
        {
            Console.WriteLine("AcNo:{0} Name:{1} LoanAmount:{2} ROI:{3}",AcNo,Name,LoanAmount,ROI);
            Console.WriteLine("Repay Amount:"+(LoanAmount+(LoanAmount*ROI)/100));
        }

        public static void Enq(double amt, int months)
        {
            double repayAmpunt = amt + (amt * ROI) / 100;
            Console.WriteLine("Loan Amount:{0} ROI:{1} RepayAmount:{2} EMI:{3} For {4} months",amt,ROI,repayAmpunt,repayAmpunt/months,months);
        }

        public static void Enq(double amt)
        {
            double repayAmpunt = amt + (amt * ROI) / 100;
            Console.WriteLine("Loan Amount:{0} ROI:{1} RepayAmount:{2} ", amt, ROI, repayAmpunt);
        }

    }
    class Program
    {
        static void Main(string[] args)
        {
            //HousingLoan.ROI = 12.8;

            HousingLoan h = new HousingLoan(123, "Peter", 56000);
            h.Display();

            HousingLoan h1 = new HousingLoan(124, "Jack", 78000);
            h1.Display();

            //HousingLoan.ROI = 13.8;

            h1.Display();
            h.Display();

            HousingLoan.Enq(68000, 12);
            HousingLoan.Enq(45000);
            
            Console.ReadLine();
        }
    }
}



THIS Keyword Eg:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace thisKeyWordEg
{
    class Customer
    {
        int AcNo;
        string Name;
        double Bal;
        string Contact;

        public Customer()
        {
            Console.WriteLine("Hello World");
        }

        public Customer(int AcNo, string Name, double Bal):this()
        {
            this.AcNo = AcNo;
            this.Name = Name;
            this.Bal = Bal;
        }

        public Customer(int AcNo, string Name, double Bal,string Contact):this(AcNo,Name,Bal)
        {
            this.Contact = Contact;
        }

        public void Deposit(double amt)
        {
            Bal = Bal + amt;
        }

        public void WithDraw(double amt)
        {
            Bal = Bal - amt;
        }

        public void BalEnq()
        {
            Console.WriteLine("AcNO:{0} Name:{1} Bal:{2} Contact:{3}", AcNo, Name, Bal, Contact);
        }

        public void BalEnq(double amt, string f)
        {
            if (f == "D")
            {
                Bal += amt;
            }
            else if (f == "W")
            {
                Bal -= amt;
            }
            else
            {
                Console.WriteLine("INVALID Flag");
            }

            Console.WriteLine("AcNO:{0} Name:{1} Bal:{2} Contact:{3}", AcNo, Name, Bal,Contact);
        }

    }

    class Program
    {
        static void Main(string[] args)
        {
            Customer c = new Customer(123, "Jack", 78000, "9676010101");

            c.BalEnq();


            Customer c1 = new Customer(124, "Peter", 89000);

            c1.BalEnq();

            Console.ReadLine();
        }
    }
}



UseDLL in ConsoleApplication:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Products;

namespace UseDllInConApp
{
    class Program
    {
        static void Main(string[] args)
        {
            int ppu;
            int nou;
            double ba;

            Console.WriteLine("Enter Price Per Unit");
            ppu = int.Parse(Console.ReadLine());

            Console.WriteLine("Enter No Of Units");
            nou = int.Parse(Console.ReadLine());

            Product p=new Product();

            ba = p.GetBillAmount(ppu, nou);

            Console.WriteLine("Your Bill Amount is "+ba);

            Console.ReadLine();
           
        }
    }
}



Use DLL in WindowsApplication:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace UseDllInWinApp
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}



Abstract Eg:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AbstractEg
{
   abstract class A
    {
        public abstract void show();

        public void Display()
        {
            Console.WriteLine("This is Display method");
        }
    }

   class B : A
   {
       public override void show()
       {
           Console.WriteLine("This is Show Method");
       }
       public void View()
       {
           Console.WriteLine("This is View Method");

        }
   }

    class Program
    {
        static void Main(string[] args)
        {
            A a;

            B b = new B();

            b.Display();
            b.View();
            b.show();

            Console.ReadLine();
        }
    }
}



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

namespace CollectionClassesEg
{
    class Program
    {
        static void Main(string[] args)
        {
            Stack S = new Stack();
            
            S.Push(12);
            S.Push(14);
            S.Push(56); 
            S.Push(34);

            //Console.WriteLine("Elements are");
            //foreach (int item in S)
            //{
            //    Console.WriteLine(item);
            //}

            //Console.WriteLine("Pop An Element");
            //Console.WriteLine(S.Pop().ToString());

            //Console.WriteLine("Elements After Poping are");
            //foreach (int item in S)
            //{
            //    Console.WriteLine(item);
            //}

            //Console.WriteLine("Pop An Element");
            //Console.WriteLine(S.Pop().ToString());

            //Console.WriteLine("Elements After Poping are");
            //foreach (int item in S)
            //{
            //    Console.WriteLine(item);
            //}

            //int sum = 0;
            //foreach (int item in S)
            //{
            //    sum = sum + item;
            //}
            //Console.WriteLine("Sum is "+sum);


            Queue Q = new Queue();

            Q.Enqueue(23);
            Q.Enqueue(45);
            Q.Enqueue(56);
            Q.Enqueue(458);
            Q.Enqueue("Manzoor");

            //Console.WriteLine("Elements are");
            //foreach (int item in Q)
            //{
            //    Console.WriteLine(item);
            //}

            //Console.WriteLine("Dequeue Element");
            //Console.WriteLine(Q.Dequeue().ToString());

            //Console.WriteLine("After Dequeue");
            //foreach (int item in Q)
            //{
            //    Console.WriteLine(item);
            //}

            int sum = 0;
            foreach (int item in Q)
            {
                sum = sum + item;
            }
            Console.WriteLine("Sum is " + sum);

            Console.ReadLine();
        }
    }
}



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

namespace ConstructorChainingEg
{
    class A
    {
        public A()
        {
            Console.WriteLine("A's Constructor with 0 Params");
        }

        public A(int x)
        {
            Console.WriteLine("A's Constructor with 1 Params");
        }
    }

    class B:A
    {
        public B():base(5)
        {
            Console.WriteLine("B's Constructor with 0 Params");
        }

        public B(int x,int y)
        {
            Console.WriteLine("B's Constructor with 2 Params");
        }
    }

    class C : B
    {
        public C():base(2,4)
        {
            Console.WriteLine("C's Constructor with 0 Params");
        }
        public C(int x)
        {
            Console.WriteLine("C's Constructor with 1 Params");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            //A a = new A();

            //B b = new B();

            C c = new C(5);

            Console.ReadLine();
        }
    }
}


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

namespace DelegatesEg
{
    class Program
    {
        delegate void Test(string name);
        
        static void Main(string[] args)
        {
            
            //Show("Manzoor");

            Test t = new Test(Show);
            //t.Invoke("Manzoor");
            //t("Manzoor");

            t += new Test(Display);
            t += new Test(Display);
            t -= new Test(Display);

            t("Manzoor");


            Console.ReadLine();
        }

        static void Show(string name)
        {
            Console.WriteLine("Show Hello "+name);
        }

        static void Display(string name)
        {
            Console.WriteLine("Display Hello " + name);
        }
    }
}


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

namespace GenericCollectionClassesEg
{
    class Student
    {
        int Sid;
        string Name;

        public Student(int Sid, string Name)
        {
            this.Sid = Sid;
            this.Name = Name;
        }

        public void Display()
        {
            Console.WriteLine("SID:{0} SName:{1}",Sid,Name);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {


            //Stack<int> S = new Stack<int>();

            //S.Push(23);
            //S.Push(89);
            //S.Push(34);

            //Console.WriteLine(S.Pop());

            //Queue<string> Q = new Queue<string>();

            //List<int> L = new List<int>();


            List<Student> S = new List<Student>();
            
            Student s1=new Student(124,"Peter");
            S.Add(s1);

            Student s2 = new Student(125, "Jack");
            S.Add(s2);

            S.Add(new Student(126,"Tom"));
            S.Add(new Student(127, "Lilly"));
            S.Add(new Student(128, "Bob"));


            foreach (Student item in S)
            {
                item.Display();
            }

            Console.ReadLine();
        }
    }
}



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

namespace InheritanceEg
{
    //Base Class
    class A
    {
       private int x;
       public int y;
       protected int z;
    }

    //Drived class
    class B:A
    {
        //protected int z;
        //public int y;
        private int l;
        public int m;

        public void Show()
        {
            //Can Access m
            //Can Access l
            //Can Access y
            //Cannot Access x
            //Can Access z
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            A a = new A();
            //Can Access   a.y;
            //Cannot Access   a.x;
            //Cannot Access a.z


            B b = new B();
            // Can Access b.m;
            // Cannot Access b.l;
            // Can Access b.y;
            // Cannot Access b.x
            // Cannot Access b.z
        }
    }
}




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

namespace InterfaceEg
{
    interface A
    {
        void Show();
        void Display(string msg);
    }
    interface B
    {
        void View();
    }
    class C:A,B
    {
        public void Show()
        {
            Console.WriteLine("This is Show Method");
        }

        public void Display(string msg)
        {
            Console.WriteLine("This is Display of "+msg);
        }
        public void View()
        {
            Console.WriteLine("This is View Method");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            //C c = new C();
            //c.Display("ManzoorTheTrainer");
            //c.Show();
            //c.View();
            A a;

            a = new C();
            a.Display("ManzoorTheTrainer");
            a.Show();
            //a.View();

            B b;
            b = new C();
            b.View();


            Console.ReadLine();
        }
    }
}




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

namespace MethodOverridingEg
{
    class Student
    {
        protected int Sid;
        protected string Name;
        protected string Standard;

        public Student()
        {
        }

        public Student(int Sid, string Name, string Standard)
        {
            this.Sid = Sid;
            this.Name = Name;
            this.Standard = Standard;
        }

        protected double Maths, Science, Social;

        public double _Maths
        {
            set { Maths = value; }
            get { return Maths; }
        }

        public double _Science
        {
            set { Science = value; }
            get { return Science; }
        }

        public double _Social
        {
            set { Social = value; }
            get { return Social; }
        }

        public virtual void GetDetails()
        {
            Console.WriteLine("SID : " + Sid);
            Console.WriteLine("Name : " + Name);
            Console.WriteLine("Standard : " + Standard);
        }

        public virtual void GetAvg()
        {
            Console.WriteLine("Avg is " +Math.Round((Maths + Science + Social) / 3.0,2));
        }
    }

    class NewStudent : Student
    {
        protected string Contact;

        public NewStudent(int Sid, string Name, string Standard,string Contact):base(Sid,Name,Standard)
        {
            //this.Sid = Sid;
            //this.Name = Name;
            //this.Standard = Standard;
            this.Contact = Contact;
        }

        protected double English;

        public double _English
        {
            set { English = value; }
            get { return English; }
        }

        public override void GetDetails()
        {
            //Console.WriteLine("SID : " + Sid);
            //Console.WriteLine("Name : " + Name);
            //Console.WriteLine("Standard : " + Standard);
            base.GetDetails();
            Console.WriteLine("Contact : " + Contact);
        }

        public override void GetAvg()
        {
            Console.WriteLine("Avg is " + Math.Round((Maths + Science + Social + English) / 4.0, 2));
        }

        public void GetDivision()
        {
            double avg = (Maths + Science + Social + English) / 4.0;
            if (avg < 35)
                Console.WriteLine("Fail");
            else if(avg<60)
                Console.WriteLine("Pass");
            else if(avg<70)
                Console.WriteLine("Passed in first Division");
            else if(avg<=100)
                Console.WriteLine("Passed in Dist");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Student s = new Student(124, "Peter", "X");
            s._Maths = 67;
            s._Science = 78;
            s._Social = 66;
            s.GetDetails();
            s.GetAvg();


            NewStudent ns = new NewStudent(125, "Tom", "IX", "9676010101");
            ns._English = 77;
            ns._Maths = 89;
            ns._Science = 55;
            ns._Social = 98;

            ns.GetDetails();
            ns.GetAvg();
            ns.GetDivision();

            Console.ReadLine();
        }
    }
}




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

namespace RuntimePolymorphismEg
{
    abstract class Employee
    {
        protected int EmpId;
        protected string EmpName;
        protected double SalPerDay;

        public Employee(int EmpId, string EmpName, double SalPerDay)
        {
            this.EmpId = EmpId;
            this.EmpName = EmpName;
            this.SalPerDay = SalPerDay;
        }

        public virtual void Display()
        {
            Console.WriteLine("EmpId : " + EmpId);
            Console.WriteLine("EmpName : " + EmpName);
            Console.WriteLine("Emp Sal Per Day : " + SalPerDay);
        }

        public abstract void CalculateSalary(int days);
    }

    class LabAssistant : Employee
    {
        protected int LabNo;
        public LabAssistant(int EmpId, string EmpName, double SalPerDay,int LabNo):base(EmpId,EmpName,SalPerDay)
        {
            this.LabNo = LabNo;
        }

        public override void Display()
        {
            base.Display();
            Console.WriteLine("LabNo :" + LabNo);
        }

        public override void CalculateSalary(int days)
        {
            double GS = SalPerDay * days;
            Console.WriteLine("Gross Salary is :"+GS);
        }
    }

    class Lecturer : Employee
    {
        protected string Sub;
        public Lecturer(int EmpId, string EmpName, double SalPerDay, string Sub)
            : base(EmpId, EmpName, SalPerDay)
        {
            this.Sub = Sub;
        }

        public override void Display()
        {
            base.Display();
            Console.WriteLine("Sub :" + Sub);
        }

        public override void CalculateSalary(int days)
        {
            double GS = (SalPerDay * days)+((SalPerDay * days)*20/100);
            Console.WriteLine("Gross Salary is :" + GS);
        }
    }

    class Admin : Employee
    {
        
        public Admin(int EmpId, string EmpName, double SalPerDay) : base(EmpId, EmpName, SalPerDay)
        {
          
        }
                
        public override void CalculateSalary(int days)
        {
            double GS = (SalPerDay * days) + ((SalPerDay * days) * 15 / 100);
            Console.WriteLine("Gross Salary is :" + GS);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Employee E;
            E = new LabAssistant(123, "Peter", 67.7, 34);

            Console.WriteLine("Enter You Choice");
            Console.WriteLine("1.LabAssistant 2.Lecturer 3.Admin 4.Exit");
            int ch = int.Parse(Console.ReadLine());

            switch (ch)
            {
                case 1:
                    E = new LabAssistant(123, "Peter", 67.7, 34);
                    break;

                case 2:
                    E = new Lecturer(124, "Tom", 89.6, "MS.Net");
                    break;

                case 3:
                    E = new Admin(126, "Lilly", 45.8);
                    break;

                case 4:
                    System.Threading.Thread.CurrentThread.Abort();
                    break;

                default:
                    break;
            }

            E.Display();
            E.CalculateSalary(25);

            Console.ReadLine();
        }
    }
}




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

namespace SealedEg
{
    abstract class A
    {
        public abstract void show();

        public void Display()
        {
            Console.WriteLine("This is Display method");
        }
    }

   sealed class B : A
    {
        public override sealed void show()
        {
            Console.WriteLine("This is Show Method");
        }
        public void View()
        {
            Console.WriteLine("This is View Method");

        }
    }

    //class C : B
    //{
    //    //public override void show()
    //    //{
    //    //    Console.WriteLine("This is Show of C");
    //    //}
    //}

    class Program
    {
        static void Main(string[] args)
        {
            

            B b = new B();

            Console.ReadLine();
        }
    }
}






Write a sample program that implements Bubble Sort in C#.NET


using System;

class myclass
{
    
static void Main()
    {
        
int[] a = { 4, 6, 9, 83, 34, 45 };
        
int temp;

        
for (int pass = 1; pass <= a.Length - 2; pass++)
        {
            
for (int i = 0; i <= a.Length - 2; i++)
            {
                
if (a[i] > a[i + 1])
                {
                    temp = a[i + 1];
                    a[i + 1] = a[i];
                    a[i] = temp;
                }

            }

        }

        
Console.WriteLine("The Sorted array");
        
foreach (int aa in a)
            
Console.Write(aa + " ");

        
Console.Read();
    }
}

Output

The Sorted array
4 6 9 34 45 83

Bubble sort using string array in C# 


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Buble_Sort_String
{
class Program
{
static void Main(string[] args)
{
string[] s = { "Bill", "Gates", "James", "Apple", "Net", "Java" };
string temp = string.Empty;
try
{
for (int i = 1; i < s.Length; i++)
{
for (int j = 0; j < s.Length-i; j++)
{
if (s[j].CompareTo(s[j + 1]) > 0)
{
temp = s[j];
s[j] = s[j + 1];
s[j + 1] = temp;
}
}
}
for (int i = 0; i < s.Length; i++)
{
Console.WriteLine(s[i] + " ");
}
Console.ReadKey();
}

catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
 
Another Model for Babble Sort :

using System;
class MainClass
{
    static void Main()
    {   
        string[] s = {"Bill", "Gates", "James", "Apple", "Net", "Java"};
        try
        {
            for(int i=0; i<s.Length; i++)
            {
                for(int j=0; j<s.Length-1; j++)
                {
                    for(int k=0; k<s.Length; k++)             
                        if(s[j][k] > s[j+1][k])
                        {
                            string temp = s[j];
                            s[j] = s[j+1];
                            s[j+1] = temp;
                        }
                
                }
            }              
        }
        catch(Exception)
        {
        } 
        for(int i=0; i<s.Length; i++ )
        {
            Console.WriteLine(s  + " ");
        }                
    }
}
Program to print Fibonacci Numbers below 100


using System;
class myclass
{
    
static void Main()
    {
        
int fn = 0;
        
int sn = 1;
        
int tn = 1;

        
Console.WriteLine(fn);
        
Console.WriteLine(sn);
        
while (true)
        {

            tn = fn + sn;
            
if (tn >= 100)
            {
                
break;
            }
            
Console.WriteLine(tn);
            fn = sn;
            sn = tn;

        }
        
Console.Read();

    }
}

Output

0
1
1
2
3
5
8
13
21
34
55
89
Fibonacci Series
using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;



namespace fib



{

    class Program

    {

          static void Main(string[] args)

        {

            int num1;

            int num2;

            num1 = num2 = 1;

            Console.WriteLine("{0}", num1);

            while (num2 < 200)

            {

                Console.WriteLine(num2);

                num2 += num1;

                num1 = num2 - num1;

            }

            Console.ReadLine();

        }

    }

}

Write program to find LCM of 2 numbers in C#.NET


using System;

public class FindLCM
{
    
public static int determineLCM(int a, int b)
    {
        
int num1, num2;
        
if (a > b)
        {
            num1 = a; num2 = b;
        }
        
else
        {
            num1 = b; num2 = a;
        }

        
for (int i = 1; i <= num2; i++)
        {
            
if ((num1 * i) % num2 == 0)
            {
                
return i * num1;
            }
        }
        
return num2;
    }

    
public static void Main(String[] args)
    {
        
int n1, n2;

        
Console.WriteLine("Enter 2 numbers to find LCM");

        n1 =
 int.Parse(Console.ReadLine());
        n2 =
 int.Parse(Console.ReadLine());

        
int result = determineLCM(n1, n2);

        
Console.WriteLine("LCM of {0} and {1} is {2}",n1,n2,result);
        
Console.Read();
    }
}

Output

Enter 2 numbers to find LCM
8
12
LCM of 8 and 12 is 24

Write a program to find GCD of 2 numbers using C#.NET


Method 1
using System;

class myclass
{
    static void Main()
    {
        int i1, i2;

        Console.WriteLine("Enter 2 numbers to find GCD");
        i1 = int.Parse(Console.ReadLine());
        i2 = int.Parse(Console.ReadLine());

        int n1, n2;
        //Making sure n1 is greater than n2
        if (i1 > i2)
        {
            n1 = i1;
            n2 = i2;
        }
        else
        {
            n1 = i2;
            n2 = i1;
        }
        int result = gcd(n1, n2);
        Console.WriteLine("The GCD of {0} and {1} is {2}",i1,i2,result);
        Console.Read();
    }

    private static int gcd(int n1, int n2)
    {
        int rem = 5;
        while (n2 > 0)
        {
            rem = n1 % n2;
            if (rem == 0)
                return n2;
            n1 = n2;
            n2 = rem;

        }
        //gcd of any number with 0 is number itself.

        return n1;

    }
}

Output

Enter 2 numbers to find GCD
125
85
The GCD of 125 and 85 is 5


Method 2

Another and interesting logic for finding  GCD of 2 numbers

using System;

class myclass
{
    
public static int euclid(int a, int b)
    {
        
if (b == 0)
            
return a;
        
else
            
return euclid(b, a % b);
    }

    
public static void Main()
    {
        
int n1 = 126, n2 = 45;
        
int gcd = euclid(n1, n2);
        
Console.WriteLine(gcd);

        
Console.Read();

    }
}


Write a Program to convert Decimal to Binary in C#.NET


using System;
class myclass
{
    
static void Main()
    {
        
int num;
        
Console.Write("Enter a Number : ");
        num =
 int.Parse(Console.ReadLine());
        
int quot;

        
string rem = "";

        
while (num >= 1)
        {
            quot = num / 2;
            rem += (num % 2).ToString();
            num = quot;
        }

        
// Reversing the  value
        
string bin = "";
        
for (int i = rem.Length - 1; i >= 0; i--)
        {
            bin = bin + rem[i];

        }

        
Console.WriteLine("The Binary format for given number is {0}",bin);


        
Console.Read();

    }
}

Output

Enter a Number : 100
The Binary format for given number is 1100100

Write a program to reverse a number using C#

using System;

namespace Learn
{
    
class Program
    {
        
static void Main(string[] args)
        {
            
Console.WriteLine("Enter a Number");
            
int numb = int.Parse(Console.ReadLine());
            
int reverse = 0;
            
while (numb > 0)
            {
                
int rem = numb % 10;
                reverse = (reverse * 10) + rem;
                numb = numb / 10;
             
            }
            
Console.WriteLine("Reverse number={0}", reverse);
            
Console.ReadLine();
        }
    }
}

Output
----------
Enter a Number
4567
Reverse number=7654

Write a program to reverse a string in C#.NET

using System;

namespace Learn
{
    
class Program
    {
        
static void Main(string[] args)
        {
            
string Str, Revstr = ""; 
            
int Length;

            
Console.Write("Enter A String : ");
            Str =
 Console.ReadLine();

            Length = Str.Length - 1;
          
            
while (Length >= 0)
            {

                Revstr = Revstr + Str[Length];
                Length--;

            }

            
Console.WriteLine("Reverse  String  Is  {0}", Revstr);

            
Console.ReadLine();

        }
    }
}

OUTPUT
-------------
Enter A String : HELLO
Reverse  String  Is  OLLEH

Find Out if a Given Number is Divisible by 3 in C#

To check if number is divisible by 3 or not you have to use the mode operator and if reminder is 0 it means it is divisible.
Find Out if a Given Number is Divisible by 3 in C#
using System;
namespace ConsoleHub
{ 
    class Programs
    {        
        static void Main(string[] args)
            {
                Console.WriteLine("Enter a Number:");
                int intDiv3 = Convert.ToInt16( Console.ReadLine());
                if (intDiv3 % 3 == 0)
                {
                    Console.WriteLine("The Number {0} is Divisible by 3", intDiv3);
                }
                else
                {
                    Console.WriteLine("The Number {0} is NOT Divisible by 3", intDiv3);
                }
                Console.ReadLine();
            }        
    }    
}

To Check whether the Entered number is polindrom or not
Palindrome number is a number that remains the same when its digits are reversed
Example: Number 151 is a palindrome number.
Number 152 - it not a palindrome number.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace palindrom
{
    class Program
    {
        static int Palindrom(int num)
        {
            int sum = 0;
            while (num != 0)
            {
                int num1 = (num % 10);
                sum = (sum * 10) + num1;
                num = num / 10;
            }
            return sum;
        }
        static void Main(string[] args)
        {
            Console.WriteLine("Enter the no:");
            int n = int.Parse(Console.ReadLine());
            int n1 = n;
            int n2 = Palindrom(n);
            if (n1 == n2)
            {
                Console.WriteLine("THE NO. IS PALINDROM");
                Console.Read();
            }
            else
            {
                Console.WriteLine("THE NO. IS NOT PALINDROM");
                Console.Read();
            }
        }
    }
}




Return Object of a Class from a Function in C#

You can return a object of a class from a method. This is simple program which is returning the object of share class. You can see that the class variable value is also returning.
Return Object of a Class from a Function in C#
using System;
namespace ConsoleHub
{
    class Share
    {
        public int ShareNumber = 299;
    }
    class Programs
    {        
        static void Main(string[] args)
            {
                Share ObjS = ObjFactory(); 
                Console.WriteLine("The Return Object is: ");
               Console.WriteLine(ObjS.ToString());
            Console.WriteLine(ObjS.ShareNumber);
                Console.ReadLine();
            }
            static Share ObjFactory()
            {
                Share objShare = new Share();
                objShare.ShareNumber = 201;
                return objShare;
            }        
    }    
}
 Admin  3:21 PM No comments: 

Return an Array from a Method in C#

This program is a example of returning an integer array from a method. Create an integer array in the function and return it. Remember that the function return type should be array of integer.
Return an Array from a Method in C#
using System;
namespace ConsoleHub
{
    class Programs
    {        
        static void Main(string[] args)
            {
                int[] ReturnArray = ArrayFactory();
                Console.WriteLine("The Return Array is: ");
                foreach (int d in ReturnArray)
                {
                    Console.Write(" {0}", d);
                }
                Console.ReadLine();
            }
            static int[] ArrayFactory()
            {
                int[] NumberQueue = { 5, 6, 3, 8, 9 };
                return NumberQueue;
            }        
    }
}
Factorial of numbers:

namespace Console_App
{
    public class clsFactorial
    {
        public static void Main()
        {
            try
            {
                Console.WriteLine("The factorial of 4 is: {0}\n",
                    Factorial(4));
            }
            catch(Exception ex)
            {
                //handle exception here
            }
            Console.ReadLine();           
        }

        static long Factorial(long number)
        {
            long FinalNumber = 1;
            for (int i = 1; i <= number; i++)
            {
                FinalNumber = FinalNumber * i;
            }
            return FinalNumber;

}
}
}

Factorial of a number:
namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            int i, n, f;
            Console.WriteLine("enter the value of n");
            n = Convert.ToInt32(Console.ReadLine());
            f = n;
            for (i = n - 1; i >= 1; i--)
            {
              f = f * i;
            }
            Console.WriteLine("Factorial of given number is :" + f);
            Console.Read();
        }
    }
}

Finding String Length in C-Sharp

Q. Write a program in C# that take string input, and print its number of characters.

string name = Console.ReadLine();
Console.WriteLine(name.Length);
Console.ReadLine();

Q. Write a program in C Sharp that take a sentense as input, and show the number of "a" used in the sentense.

string name = Console.ReadLine();
int counter = 0;
for (int i = 0; i < name.Length; i++)
{
if (name[i] == 'a')
{
counter++;
}
}
Console.WriteLine(counter);
Console.ReadLine();

Q. Write a program in C# taht take name and password. If the name and password are correct, the program show "you are logged in", otherwise, "incorrect name or password".

Console.WriteLine("Enter your Name");
Console.WriteLine("Enter your Pswrd");
string name = Console.ReadLine();
string pswrd = Console.ReadLine();
string myname = "bilal";
string mypswrd = "123456";
if (name == myname && pswrd == mypswrd)
{
Console.WriteLine("You are logged in");
}
else
{
Console.WriteLine("Incorrect name or pswrd");
}
Console.ReadLine();
Sorting Arrays in C Sharp
Q. Write a string array of length 3, and sort them.

string[] name = new string[] { "We", "He", "Us"};
Array.Sort(name);
foreach (string i in name)
{
Console.WriteLine(i);
}
Console.ReadLine();

Q. Write a string array in C# that take 5 inputs, and sort them.

string[] name = new string[5];
for (int i = 0; i < 5; i++)
{
name[i] = Console.ReadLine();
}
Array.Sort(name);
foreach (string i in name)
{
Console.WriteLine(i);
}
Console.ReadLine();

Q. Write an array in C Sharp of length 3, and sort it.

int[] numbers = new int [] { 4, 3, 8, 0, 5 };
Array.Sort(numbers);
foreach (int i in numbers)
{
Console.WriteLine(i);
}
Console.ReadLine();

Q. Write a program in C# that take 5 integers, and sort them.

int[] numbers = new int[5];
for (int i = 0; i < 5; i++)
{
numbers[i] = Convert.ToInt16(Console.ReadLine());
}
Array.Sort(numbers);
foreach (int i in numbers)
{
Console.WriteLine(i);
}
Console.ReadLine();
Print Pattern in C-Sharp
Q. Print * 10 times vertically usinf C# Console Application.

for (int i = 1; i < 11; i++)
{
Console.WriteLine("*");
}
Console.ReadLine();

Q. Print * 10 times Horizontally usinf C# Console Application.

for (int i = 1; i < 11; i++)
{
Console.Write("*");
}
Console.ReadLine();

Q. Print * 10 times Horizontally with spaces between them usinf C# Console Application.

for (int i = 1; i < 11; i++)
{
Console.Write("* ");
}
Console.ReadLine();

Q. Write a program in C# that take string input and print the result vertically.

string name = Console.ReadLine();
for (int i = 0; i < name.Length; i++)
{
Console.WriteLine(name[i]);
}
Console.ReadLine();

Q. Print the following pattern using C# Console Application.
*
**
***
****
*****


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

Q. Print the following pattern using C-Sharp Console Application.
*****
****
***
**
*


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

Q. Print pyramid using C# Console Application, like this:
    *
   * *
  * * *
 * * * *
* * * * *
for (int i = 1; i < 6; i++)
{
for (int j = 4; j >= i; j--)
{
Console.Write(" ");
}
for (int k = 1; k <= i; k++)
{
Console.Write("* ");
}
Console.WriteLine("");
}
Console.ReadLine();
1. How to write on screen in C#.
Console.WriteLine("I have been studying C# for 4 weeks!");
This will appear on screen, and will disappear immediately. So, We are writing another statement.
Console.WriteLine("I have benn studying C# for 4 weeks!");
Console.ReadLine();
Now it will remain on screen, unless we press Enter key.
Here we see that we can write letters, numbers and special characters (#) on screen.

String: String is name of variable, and it could be anything letters, numbers and special charecters. We can use string for numbers also, but when we need any arithmetical operation, we use integers, decimals etc as variables

2. String Concatenation:
We can add two strings, called string concatenation. For example, we are adding first name and second name to get full name.
string a = "John ";
string b = "Smith";
string c = a + b;
Console.WriteLine(c);
Console.ReadLine();
3. Adding two numbers:
int a=10;
int b=12;
int c=a+b;
Console.WriteLine(c);
Console.ReadLine();
Here, we use integers as variables, because we want to add them (arithmetical operation).
Integers does NOT take fractional (decimal) values. If we want to perform arithmetical operation of fractional values; we can take double as variables.
double a = 3.4;
double b = 5.2;
double c = a + b;
Console.WriteLine(c);
Console.ReadLine();
4. How C# take input:
The program will take input from user, and will display it on screen
string name = Console.ReadLine();
Console.WriteLine(name);
Console.ReadLine();
Integer as input:
Input is always in string. So for integer, we need to convert it first.
int number = Convert.ToInt16(Console.ReadLine());
Console.WriteLine(number);
Console.ReadLine();
Remember: As the input is always in string, so for integers as input we need to convert it first.

5. Take two inputs (integers) from user and add them.
int number1 = Convert.ToInt16(Console.ReadLine());
int number2 = Convert.ToInt16(Console.ReadLine());
int number3 = number1 + number2;
Console.WriteLine(number3);
Console.ReadLine();
6. Take two inputs (string) from user, and add them.
string firstname = Console.ReadLine();
string lastname = Console.ReadLine();
string fullname = firstname + lastname;
Console.WriteLine(fullname);
Console.ReadLine();
As, input is always in string, so we did not need to convert it.

Conditional Statements in C#:
·        The if, if / else, if / else if / else Statement
·        The switch Statement

The if Statement:
Construction:
if (condition)
{statement}
For example,
int a = Convert.ToInt16(Console.ReadLine());
if (a > 10)
{Console.WriteLine("The number is greater than 10");
Console.ReadLine();
The if / else Statement:
Construction:
if (condition)
{statement}
else
{statement}
For example,
int a = Convert.ToInt16(Console.ReadLine());
if (a > 10)
{Console.WriteLine("The number is greater than 10");}
else
{ Console.WriteLine("The number is 10 or less than 10");}
Console.ReadLine();
The if / else if / else Statement- (also called nested if)
Construction:
if (condition)
{statement}
else if (condition)
{statement}
else
{statement}
For example,
int a = Convert.ToInt16(Console.ReadLine());
if (a > 10)
{Console.WriteLine("The number is greater than 10");}
else if (a == 10)
{Console.WriteLine("The number is 10");}
else
{ Console.WriteLine("The number is less than 10");}
Console.ReadLine();
NOTE: We write = two times.

The switch Statement:
Construction:
switch (integer a)
{
case 1:
statement
break;
case 2:
statement
break;
default:
statement
break;
}
NOTE: The default in switch statement is equaivalent to else in if statement.
For example,
int week = Convert.ToInt16(Console.ReadLine());
switch (week)
{
case 1:
Console.WriteLine("Monday");
break;
Console.WriteLine("Tuesday");
break;
case 3:
Console.WriteLine("Wednesday");
break;
case 4:
Console.WriteLine("Thursday");
break;
case 5:
Console.WriteLine("Friday");
break;
case 6:
Console.WriteLine("Saturday");
break;
case 7:
Console.WriteLine("Sunday");
break;
default:
Console.WriteLine("NOT KNOWN");
break;
}
Console.ReadLine();
The for loop in C#
Construction
for (initial point; ending point; increament)
{
Statement(s)
}
For example, the following program will write counting from 1 to 20.
for (int i = 1; i < 21; i++)
{
Console.WriteLine(i);
}
Console.ReadLine();
Q. Write table of 2 using for loop in C#.
for (int i = 1; i < 11; i++)
{
int tab = 2 * i;
Console.WriteLine(tab);
}
Console.ReadLine();
Q. Write a program that print even numbers from 1 to 100.
for (int i = 1; i < 101; i++)
{
if (i % 2 == 0)
{
Console.WriteLine(i);
}
}
Console.ReadLine();
Q. Write a program that take input from user, and write table of that number.
Console.WriteLine("Enter a number:");
int num = Convert.ToInt16(Console.ReadLine());
for (int i = 1; i < 11; i++)
{
int tab = i * num;
Console.WriteLine(tab);
}
Console.ReadLine();
Q. Write a program in C sharp, to find the factorial of 5.
int fact = 1;
for (int i = 5; i > 0; i--)
{
fact=fact*i;
}
Console.WriteLine(fact);
Console.ReadLine();
Q. Write a program that take input from user, and find factorial of that number.
Console.WriteLine("Enter a number:");
int num = Convert.ToInt16(Console.ReadLine());
Console.WriteLine("Its factorial is:");
int fact = 1;
for (int i = num; i > 0; i--)
{
fact=fact*i;
}
Console.WriteLine(fact);
Console.ReadLine();
The while Loop
Construction:
while (condition)
{
statement(s)
}
Q. Write a program in C# using while loop, that take a number from user and return cube of that number. And the program ends when the input is 11.
int num = Convert.ToInt16(Console.ReadLine());
int cube = num * num * num;
while (num != 11)
{
Console.WriteLine(cube);
break;
}
Console.ReadLine();
Q. Write a program that starts from 0, increase 1 by 1, and end before 10 using while loop in C Sharp.
int number = 0;
while (number < 10)
{
Console.WriteLine(number);
number = number + 1;
}
Console.ReadLine();
The do - while loop
Construction
do
{
statement(s)
}
while
{
statement(s)
}
Q. Write a program in C Sharp using do - while loop, that take a number, and increase it 2 by 2, and ends before 30.
int num = Convert.ToInt16(Console.ReadLine());
do
{
Console.WriteLine(num);
num = num + 2;
}
while (num < 30);
Console.ReadLine();
Array in C#
Construction
variable type [] variable name = new variable type [length]
Array of type integer with constant values
int[] myarray = new int[3] { 2, 5, 9 };
Console.WriteLine(myarray[0]);
Console.ReadLine();
NOTE:index 0 inside Console.WriteLine statement represents index of array, that is 2.
In the above myarray; index 0 = 2, index 1 = 5, index 2 = 9.
If we replace 0 by 1, the program will show 5, and for 2, the program will show 9.

Array of type string with constant values
string[] name = new string [3] { "Bilal", "Sohail", "Afzal" };
Console.WriteLine(name[0]);
Console.ReadLine();
Q. Write an array in C# of type integer that take 3 numbers as input (the program must close after taking 3 inputs).
int[] myarray = new int[3];
myarray[0] = Convert.ToInt16(Console.ReadLine());
myarray[1] = Convert.ToInt16(Console.ReadLine());
myarray[2] = Convert.ToInt16(Console.ReadLine());
Console.WriteLine(myarray);
Console.ReadLine();
Q. Write an array in C Sharp of type string that take 3 strings (names) as input (the program must ends after taking 3 inputs).
string[] name = new string[3];
name[0] = Console.ReadLine();
name[1] = Console.ReadLine();
name[2] = Console.ReadLine();
Console.WriteLine(name);
Console.ReadLine();
Q. Write an array in C# of type integer that take 10 numbers as input (Use for loop for simplicity).
int[] myarray = new int[10];
for (int i = 0; i < 10; i++)
{
myarray[i] = Convert.ToInt16(Console.ReadLine());
}
Console.WriteLine(myarray);
Console.ReadLine();
Q. Write a program in C Sharp that take 10 inputs from user, and show their sum.
int[] myarray = new int[10];
for (int i = 0; i < 10; i++)
{
myarray[i] = Convert.ToInt16(Console.ReadLine());
}
int a = 0;
for (int j = 0; j < 10; j++)
{
a = a + myarray[j];
}
Console.WriteLine(a);
Console.ReadLine();
Q. Write a program in C# that take 10 numbers, and show their average (input could be decimal or fractional value also).
double[] myarray = new double[10];
for (int i = 0; i < 10; i++)
{
myarray[i] = Convert.ToInt16(Console.ReadLine());
}
double a = 0;
double b = 0;
for (int j = 0; j < 10; j++)
{
a = a + myarray[j];
b = a / 10;
}
Console.WriteLine(b);
Console.ReadLine();
Introduction to Windows Forms Application

Q. Take two inputs from user (first name and second name) and concatenates them and print it, using windows forms application in C#.
For this, take 2 textboxes and 1 button from toolbox menue.
string firstname = textBox1.Text;
string secondname = textBox2.Text;
string fullname = firstname + secondname;
MessageBox.Show(fullname);
Q. Take 2 inputs from user and add them using windows forms application in C#.
int a = Convert.ToInt16(textBox1.Text);
int b = Convert.ToInt16(textBox2.Text);
int c = a + b;
MessageBox.Show(Convert.ToString(c));
Q. Write a program that take input from user and show whether the number is odd or even, using windows forms application in C Sharp.
For this, take 1 textbox and 1 button.
int number = Convert.ToInt16(textBox1.Text);
if (number % 2 == 0)
{
MessageBox.Show("EVEN");
}
else
{
MessageBox.Show("ODD");
}
Functions in C#
Construction
output type (input type)
{
return (program);
}

Now call the function

output type = function name;
The following example will make the matter clear.

Q. Write a function that take 2 numbers and add them using windows forms application in C#.
Function
int add(int a, int b)
{
return (a + b);
}
Now, call the function
int c = add(Convert.ToInt16(textBox1.Text), Convert.ToInt16(textBox2.Text));
MessageBox.Show(Convert.ToString(c));
Q. Write a function in C Sharp which takes three number as input parameter and return the largest of three.
Function
int largest(int a, int b, int c)
{
if (a > b && a > c)
{
return a;
}
else if (b > a && b > c)
{
return b;
}
else
{
return c;
}
}
Call the function
int result = largest(Convert.ToInt16(textBox1.Text), Convert.ToInt16(textBox2.Text), Convert.ToInt16(textBox3.Text));
MessageBox.Show(Convert.ToString(result));
Q. Write a program in C# that take Temperature in Fahrenheight, and convert it to Centigrate.
Console.WriteLine("Enter Temperature in Fahrenheight:");
double ftemp = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Equivalent Temperature in Centigrate is:");
double ctemp= (ftemp-32) * 5 / 9;
Console.WriteLine(ctemp);
Console.ReadLine();
Q. Write a program in C Sharp that take Month and Date, and show number of days from the start of the year to that date.
Console.WriteLine("Enter Month");
Console.WriteLine("Enter Date");
int b = Convert.ToInt16(Console.ReadLine());
int c = Convert.ToInt16(Console.ReadLine());
Console.WriteLine("Number of days from the start of the year are:");
int a = 0;
int d = 0;
int[] month = new int[12] { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
for (int i = 0; i < b - 1; i++)
{
a = a + month[i];
d = a + c;
}
Console.WriteLine(d);
Console.ReadLine();
Finding String Length in C-Sharp

Q. Write a program in C# that take string input, and print its number of characters.

string name = Console.ReadLine();
Console.WriteLine(name.Length);
Console.ReadLine();

Q. Write a program in C Sharp that take a sentense as input, and show the number of "a" used in the sentense.

string name = Console.ReadLine();
int counter = 0;
for (int i = 0; i < name.Length; i++)
{
if (name[i] == 'a')
{
counter++;
}
}
Console.WriteLine(counter);
Console.ReadLine();

Q. Write a program in C# taht take name and password. If the name and password are correct, the program show "you are logged in", otherwise, "incorrect name or password".

Console.WriteLine("Enter your Name");
Console.WriteLine("Enter your Pswrd");
string name = Console.ReadLine();
string pswrd = Console.ReadLine();
string myname = "bilal";
string mypswrd = "123456";
if (name == myname && pswrd == mypswrd)
{
Console.WriteLine("You are logged in");
}
else
{
Console.WriteLine("Incorrect name or pswrd");
}
Console.ReadLine();
Sorting Arrays in C Sharp
Q. Write a string array of length 3, and sort them.

string[] name = new string[] { "We", "He", "Us"};
Array.Sort(name);
foreach (string i in name)
{
Console.WriteLine(i);
}
Console.ReadLine();

Q. Write a string array in C# that take 5 inputs, and sort them.

string[] name = new string[5];
for (int i = 0; i < 5; i++)
{
name[i] = Console.ReadLine();
}
Array.Sort(name);
foreach (string i in name)
{
Console.WriteLine(i);
}
Console.ReadLine();

Q. Write an array in C Sharp of length 3, and sort it.

int[] numbers = new int [] { 4, 3, 8, 0, 5 };
Array.Sort(numbers);
foreach (int i in numbers)
{
Console.WriteLine(i);
}
Console.ReadLine();

Q. Write a program in C# that take 5 integers, and sort them.

int[] numbers = new int[5];
for (int i = 0; i < 5; i++)
{
numbers[i] = Convert.ToInt16(Console.ReadLine());
}
Array.Sort(numbers);
foreach (int i in numbers)
{
Console.WriteLine(i);
}
Console.ReadLine();
Print Pattern in C-Sharp
Q. Print * 10 times vertically usinf C# Console Application.

for (int i = 1; i < 11; i++)
{
Console.WriteLine("*");
}
Console.ReadLine();

Q. Print * 10 times Horizontally usinf C# Console Application.

for (int i = 1; i < 11; i++)
{
Console.Write("*");
}
Console.ReadLine();

Q. Print * 10 times Horizontally with spaces between them usinf C# Console Application.

for (int i = 1; i < 11; i++)
{
Console.Write("* ");
}
Console.ReadLine();

Q. Write a program in C# that take string input and print the result vertically.

string name = Console.ReadLine();
for (int i = 0; i < name.Length; i++)
{
Console.WriteLine(name[i]);
}
Console.ReadLine();

Q. Print the following pattern using C# Console Application.
*
**
***
****
*****


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

Q. Print the following pattern using C-Sharp Console Application.
*****
****
***
**
*


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

Q. Print pyramid using C# Console Application, like this:
    *

   * *

  * * *

 * * * *

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