Sunday, July 1, 2012

factorial number c#


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

namespace factorial
{
    class Program
    {
        static void Main(string[] args)
        {
         
            int fact = 1, i,n;
            Console.WriteLine("Enter any no. for factorial");
            n = int.Parse(Console.ReadLine());
            for (i = n; i >= 1; i--)
            {
                fact = fact * i;
            }

            Console.WriteLine("factorial value is:"+fact);
            Console.ReadKey();      
         }
    }
}

output

Enter any no. for factorial
7
factorial value is:5040

Program for prime number in c#


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

namespace prime_num
{
    class Program
    {
        static void Main(string[] args)
        {
            int f = 0;
            int[] primenum = new int[100];
            int i, j;
            Console.WriteLine("Prime no is:");
            for (i = 2; i != primenum.Length; i++)
            {
                for (j = 2; j != i; j++)
                {

                    if (i % j == 0)
                    {
                        f = 0;
                        break;

                    }
                    f = 1;
                }

                if (f == 1)
                {
                    primenum[i] = i;
                    Console.WriteLine("" + primenum[i]);
                }
            }

            Console.ReadKey();
        }
    }

}

output



Prime no is:
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97






Sunday, June 24, 2012

program for finding mark sheet individual subject marks ,total and average using set and get


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

namespace r_w_property
{
    class marksheet
    {
        private int math;
        private int phy;
        private int che;
        private int eng;
        public int math_marks
        {
            get
            {
                return math;
            }
            set
            {
                math = value;
            }
        }
        public int phy_marks
        {
            get
            {
                return phy;
            }
            set
            {
                phy = value;
            }
        }
        public int eng_marks
        {
            get
            {
                return eng;
            }
            set
            {
                eng = value;
            }
        }
        public int che_marks
        {
            get
            {
                return che;
            }
            set
            {
                che = value;
            }
        }
       
    }
    class Program
    {
        static void Main(string[] args)
        {
            marksheet m= new marksheet();
            m.math_marks=45;
            m.phy_marks=46;
            m.che_marks=47;
            m.eng_marks=43;
            Console.WriteLine
            (
            @"student individual subject marks:
math marks={0}
phy marks={1}
che marks={2}
eng marks={3} ",m.math_marks,
            m.phy_marks,
            m.che_marks,
            m.eng_marks);
            int total = m.math_marks + m.phy_marks + m.che_marks + m.eng_marks;
            Console.WriteLine("total marks is:"+total);
            float avg = total / 4;
            Console.WriteLine("Avrage marks is:"+avg);
            Console.ReadKey();

        }
    }
}

output



student individual subject marks:
math marks=45
phy marks=46
che marks=47
eng marks=43
total marks is:181
Avrage marks is:45



Saturday, June 23, 2012

create class library and call it in basic program in c#

Go on File -- New -- Project -- choose Class library --  Give your library name--ok


using System;

namespace library
{
    public class lib
    {
        static public int sum(int a, int b)
        {
            return (a + b);
        }
    }
}

  • Then compile it


Now call it in basic program :

Go on File -- New -- Project -- choose Console Application -- Give your   Console Application _name--ok

For setting
Go on Project -- Add Reference -- Browse -- choose location where Console Application save -- bin-- Debug--  library_name.dll

using System;

namespace class_sum_library
{
    class Program
    {
        static void Main(string[] args)
        {
            int a, b, c;
            Console.WriteLine("Enter number for a:");
            a = int.Parse(Console.ReadLine());
            Console.WriteLine("Enter number for b:");
            b = int.Parse(Console.ReadLine());
            c = a + b;
            library.lib.sum(a, b);
            Console.WriteLine("Sum is:" + c);
            Console.ReadKey();
            
        }

    }
}

output



Enter number for a:
400
Enter number for b:
234
Sum is:634

how to impement interface in c#


using System;

    interface pnr
    {

        void pnr();
     
    }
    interface si : pnr
    {
        void simple_intrest();
    }



    class result : si
    {
        static void Main(string[] args)
        {

            result s = new result();

            s.pnr();
            s.simple_intrest();
            Console.ReadKey();

        }


        public void simple_intrest()
        {
            int p, n, r;
            Console.WriteLine("\nEnter value for principle amount");
            p = int.Parse(Console.ReadLine());
            Console.WriteLine("Enter value for rate");
            r = int.Parse(Console.ReadLine());
            Console.WriteLine("Enter value for time");
            n = int.Parse(Console.ReadLine());
            int si = p * n * r / 100;
            Console.WriteLine("Simple interest is:{0} for principle amount is:{1}", si, p);
        }
        public void pnr()
        {
            Console.WriteLine("Enter the value for p,n,r");


        }

    }
Output

Enter the value for p,n,r

Enter value for principle amount
20000
Enter value for rate
5
Enter value for time
4
Simple intrest is:4000 for principle amount is:20000

Thursday, June 21, 2012

inheritance program in c#


using System;

namespace inhetance
{
    class cal
    {
        public int a, b, c;
        public int sum(int x, int y)
        {
            a = x;
            b = y;
            c = a + b;
            return (c);
        }
        public int sub(int x, int y)
        {
            a = x;
            b = y;
            c = a - b;
            return (c);
        }
        public int div(int x, int y)
        {
            a = x;
            b = y;
            c = a / b;
            return (c);
        }
        public int mul(int x, int y)
        {
            a = x;
            b = y;
            c = a * b;
            return (c);
        }
    }


       class scal : cal
       {
           public void factorial(int n)
           {
               int i,fact = 1;
             
             
               for (i = n; i >= 1; i--)
               {
                   fact = fact * i;
               }
               Console.WriteLine("factorial value of n is:" +fact);
           }
       }
 
    class Program
    {
        static void Main(string[] args)
        {
            cal x = new cal();
            int a, b,c;
            Console.WriteLine("Enter the number for a & b=:");
            a = int.Parse(Console.ReadLine());
            b = int.Parse(Console.ReadLine());
            x.sum(a, b);
            x.sum(a,b);
            x.mul(a, b);
            x.div(a, b);
            Console.WriteLine("sum of a & b is"+x.sum(a,b));
            Console.WriteLine("sum of a & b is" + x.sub(a, b));
            Console.WriteLine("sum of a & b is" + x.mul(a, b));
            Console.WriteLine("sum of a & b is" + x.div(a, b));
         
            scal x1 = new scal();
            int n;
            Console.WriteLine("Enter no for factorial value:");
            n = int.Parse(Console.ReadLine());
            x1.factorial(n);
         
            Console.ReadKey();

        }
    }
}


Output:


Enter the number for a & b=:
12
3
sum of a & b is15
sum of a & b is9
sum of a & b is36
sum of a & b is4
Enter no for factorial value:
7
factorial value of n is:5040

find simple interest c#


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

namespace intrest
{
    class si
    {
        public int p, n, r;
         public int getdata(int x, int y, int z)
        {
            int b;
            p = x;
            n = y;
            r = z;
             b= p * n * r / 100;
             return(b);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            int a, b,c;
            si i = new si();
         
            Console.WriteLine("Enter principal amount:");
            a = int.Parse(Console.ReadLine());
            Console.WriteLine("Enter rate:");
            b = int.Parse(Console.ReadLine());
            Console.WriteLine("Enter time in year:");
            c = int.Parse(Console.ReadLine());

            int simple_intrest = i.getdata(a,b,c);
            Console.WriteLine("simple intrest={0} of principal={1}", simple_intrest,a);
            Console.ReadKey();

        }
    }

}

Output

Enter principal amount:
20000
Enter rate:
5
Enter time in year:
4
simple intrest=4000 of principal=20000