Wednesday, August 18, 2010

sample program to demonstrate the use of structures.


1. Write a sample program to demonstrate the use of structures.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace @struct
{
    class Program
    {
        static void Main(string[] args)
        {
            CustomerDetails obj = new CustomerDetails();
            Console.WriteLine("Enter the CustomerID");
            obj.customerid = Console.ReadLine();
            Console.WriteLine("Enter the CustomerName");
            obj.customername = Console.ReadLine();
            Console.WriteLine("Enter the CustomerPhoneNo");
            obj.customerphoneno = Console.ReadLine();
            Console.WriteLine("Enter the CustomeAddress");
            obj.customeraddress = Console.ReadLine();
            Console.WriteLine("Enter the ProductName");
            obj.customerproduct = Console.ReadLine();
            Console.WriteLine("Enter the BillAmount");
            obj.customerbillamt = Console.ReadLine();
            Console.WriteLine("Customer Information is as follows:");
            Console.WriteLine("===================================");
            Console.WriteLine("CustomerID:\t{0}", obj.customerid);
            Console.WriteLine("CustomerName:\t{0}", obj.customername);
            Console.WriteLine("CustomerPhone:\t{0}", obj.customerphoneno);
            Console.WriteLine("CustomerAddress:\t{0}", obj.customeraddress);
            Console.WriteLine("CustomerProduct:\t{0}", obj.customerproduct);
            Console.WriteLine("CustomerBillAmount:\t{0}", obj.customerbillamt);
            Console.ReadLine();
        }
    }
    struct CustomerDetails
    {
        public string customerid;
        public string customername;
        public string customerphoneno;
        public string customeraddress;
        public string customerproduct;
        public string customerbillamt;
    }

}

Tuesday, August 17, 2010

Sample program to understand enum.


Write a sample program to understand enum.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Enums
{
    class Program
    {
        enum Days { sat, sun, mon, tue, wed, thu, fri };
        static void Main(string[] args)
        {
            Console.WriteLine("{0}",(int)Days.sun);
            Console.ReadLine();
        }
    }
}

Saturday, August 14, 2010

Examples on Value Types and Reference Types


3.Program1:

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

namespace Csharp
{
    /// <summary>
    /// 1.Value Types  Struct
    /// </summary>
    class ValuetypeStruct
    {
        static void Main(string[] args)
        {
            Person p = new Person("Tony", "Allen", 32);
            Console.Write(p.ToString());
            Console.ReadLine();
        }
        struct Person
        {
            public string firstName;
            public string lastName;
            public int age;

            public Person(string _firstName, string _lastName, int_age)
            {
                firstName = _firstName;
                lastName = _lastName;
                age = _age;
            }

            public override string ToString()
            {
                return firstName + lastName + age;
            }
        }

    }
}





4.Program 2:

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

namespace Csharp
{
    /// <summary>
    /// Extending program1 to make use of enum
    /// </summary>
    class Enum
    {
        static void Main(string[] args)
        {
            Person p = new Person("Tony", "Allen", 32, Person.Genders.Male);
            Console.Write(p.ToString());
            Console.ReadLine();
        }

        struct Person
        {
            public string firstName;
            public string lastName;
            public int age;
            public Genders gender;

            public Person(string _firstName, string _lastName, int _age, Genders _gender)
            {
                firstName = _firstName;
                lastName = _lastName;
                age = _age;
                gender = _gender;
            }

            public override string ToString()
            {
                return firstName + " " + lastName + " ," + gender + ", age " + age;
            }

            public enum Genders : int { Male, Female };
        }

    }
}






5.Program 3:

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

namespace Csharp
{
    /// <summary>
    /// Example of Reference Type
    /// </summary>
    class Referencetypeclass
    {
        static void Main(string[] args)
        {
            Person p = new Person("Tony", "Allen", 32, Person.Genders.Male);
            Console.Write(p.ToString());
            Console.ReadLine();
        }

        class Person
        {
            public string firstName;
            public string lastName;
            public int age;
            public Genders gender;

            public Person(string _firstName, string _lastName, int _age, Genders _gender)
            {
                firstName = _firstName;
                lastName = _lastName;
                age = _age;
                gender = _gender;
            }

            public override string ToString()
            {
                return firstName + " " + lastName + " ," + gender + ", age " + age;
            }

            public enum Genders : int { Male, Female };
        }
    }
}

Friday, August 13, 2010

For Loop Demo C#


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

namespace Csharp
{
    class forloop
    {
        public static void Main(string[] args)
        {
            implement obj = new implement();
            obj.forloop();
            Console.ReadLine();
        }
    }
    class implement
    {

        public void forloop()
        {
            Console.WriteLine("The Output is displayed by using Console.WriteLine()\n");
               for(int i=0; i<5; i++)
               {
                   Console.WriteLine("{0}", i);
               }

            Console.WriteLine("\nThe Output is displayed by using Console.Write()\n");
               for (int i = 0; i < 5; i++)
               {
                   Console.Write("{0}\t", i);
               }
        }

    }
}

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

namespace Csharp
{
    /// <summary>
   
    /// </summary>
    class Extendingforloop
    {
        public static void Main(string[] args)
        {
            design obj = new design();
            obj.loop1();
            obj.loop2();
            obj.loop3();
            obj.loop4();
            //obj.loop5();
            Console.ReadLine();
        }
    }
    class design
    {
        public void loop1()
        {
            for (int i = 1; i <= 5; i++)
            {
                for (int j = 1; j <= i; j++)
                {
                    Console.Write("{0}",i);
                }
                Console.Write("\n");
            }
            Console.Write("\n");
        }
        public void loop2()
        {
            for (int i = 1; i <= 5; i++)
            {
                for (int j = 1; j <= i; j++)
                {
                    Console.Write("{0}", j);
                }
                Console.Write("\n");
            }
            Console.Write("\n");
        }
        public void loop3()
        {
            for (int i = 5; i >= 1 ; i--)
            {
                for (int j= 5;j >= i;j--)
                {
                    Console.Write("{0}", j);
                    if (i <1)
                        break;
                }
                Console.Write("\n");
            }
            Console.Write("\n");
        }
        public void loop4()
        {
            int i=6, c = 0;
            for (; i >= 0; i--)
            {
                c = i;
                Console.Write("\n");
                for (; ; )
                {
                    Console.Write("{0}", c);
                    if (c == 0)
                        break;
                    c--;

                }
            }

        }

Thursday, August 12, 2010

Program to design calculator using switch case.


Write a program to design calculator using switch case.

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

namespace Csharp
{
    /// <summary>
    /// Simple Switch Case Example
    /// </summary>
    class SimpleSwithch
    {
        public static void Main(string[] args)
        {
            Calculator obj = new Calculator();
            obj.cal();
            Console.ReadLine();
        }
    }
    class Calculator
    {
        public int a, b, sum,diff,mul,div;
        public char c;
        public void cal()
        {
        menu:
            Console.Clear();
            Console.WriteLine("=======================================");
            Console.WriteLine("Menu");
            Console.WriteLine("1.Addition");
            Console.WriteLine("2.Subtraction");
            Console.WriteLine("3.Multiplication");
            Console.WriteLine("4.Division");
            Console.WriteLine("=======================================");
         
            Console.WriteLine("Enter Your Option");
            c = Convert.ToChar(Console.ReadLine());
            Console.WriteLine("Enter the Values of a,b");
            a = Convert.ToInt32(Console.ReadLine());
            b = Convert.ToInt32(Console.ReadLine());
            switch (c)
            {
                case '1':
                    sum = a + b;
                    Console.WriteLine("{0}", sum);
                    break;
                case '2':
                    diff = a - b;
                    Console.WriteLine("{0}", diff);
                    break;
                case '3':
                    mul = a * b;
                    Console.WriteLine("{0}", mul);
                    break;
                case '4':
                    div = a / b;
                    Console.WriteLine("{0}", div);
                    break;
                   
            }
            Console.WriteLine("Enter M for Menu or Any other key to Exit");
            c = Convert.ToChar(Console.ReadLine());
            if (c == 'M')
            {
                goto menu;
            }
        }
    }
}

program to calculate total interest based on given conditions using if condition.


Write a program to calculate total interest based on given conditions using if condition.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Csharp
{
    
    /// <summary>
    /// Calculating Total interest based on the given conditions:
    /// Principle Amount     Rate of Interest
    /// >=10,000                20%
    /// >=8000&&<=9999          18%
    /// <8000                   16%
    /// </summary>
    class IFCondition
    {
        public float princ, nyrs, rate, interest;
        public static void Main(string[] args)
        {
            IFCondition obj = new IFCondition();
            obj.res();
            Console.ReadLine();
        }


        public void res()
        {
            Console.Write("\n Enter Loan and No of Years");
            princ = Convert.ToInt32(Console.ReadLine());
            nyrs = Convert.ToInt32(Console.ReadLine());
            if (princ >= 10000)
            {
                rate = 20;
            }
            else if (princ >= 8000 && princ <= 9999)
            {
                rate = 18;
            }
            else if (princ < 8000)
            {
                rate = 16;
            }
            interest = princ * nyrs * rate / 100;
            Console.Write("Years:{0}", nyrs);
            Console.Write("Loan:{0}", princ);
            Console.Write("Rate of Interest:{0}", rate);
            Console.Write("Interest Amount:{0}", interest);
        }
    }
}