Showing posts with label Programs. Show all posts
Showing posts with label Programs. Show all posts

C# Sample Programs


1.Accept Simple Point from the keyboard and check whether it is a origin on x axis , on y axis or in 1st quadrant or in 2nd or in 3rd or in 4th quadrant where exactly the point can be?

Click Here to see the Code

2.Write a Simple Program to check whether the given point is on the surface of the circle or inside the circle or outside the circle?

Click here to see the code

3.Write a simple program to accept student number,name,marks of 3 subjects. Find the average,total and grade in each subject. And Print the total marks,avg, grade of the student , if he/she got less than 40marks in any subject he/she is failure. 

Click Here to see the Code

4.Write a c# program to find sum of digits in a given number.

Click Here to see the code


5.Define a Method to reverse the given number

Click Here to see the Code


6.Define a method to generate fibanacii series

Click Here to See the Code


7.Define a method to find sum of even numbers up to given limit

Click Here to see the Code


8.Define a method to find factorial of a Given Number

Click Here to see the Code
 

8. Finding the Factorial

8.Define a method to find factorial of a Given Number

Code:

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

namespace factoriald
{
    class Program
    {
        public static void Fact(int num)
        {
            int i, factorial = 1;
           
                for (i = 1; i <= num; i++)
                {
                    factorial = factorial * i;
                }
                Console.WriteLine("FACTORIAL= " + factorial);
        }
        static void Main(string[] args)
        {
            int num;
            Console.WriteLine("ENTER THE NUMBER");
            num = int.Parse(Console.ReadLine());
            if (num < 0)
                Console.WriteLine("Factorial not possible");
            else if (num == 0)
                Console.WriteLine("Factorial is 1"); 
            else
               Fact(num);
           
        }
    }
}
 
 

7. Sum of Even Number Program

7.Define a method to find sum of even numbers up to given limit

Code:

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

namespace Evensum
{
    class Program
    {
        public static int Even(int n)
        {
            int i, sumeven = 0;
            for (i = 1; i <= n; i++)
            {
                if ((i % 2) == 0)
                {
                    sumeven = sumeven + i;
                }
            }
            return sumeven;
           

        }
        static void Main(string[] args)
        {
            int n,ans;
            Console.WriteLine("ENTER THE LIMIT");
            n = int.Parse(Console.ReadLine());
            ans=Even(n);
            Console.WriteLine("Sum of Even Numbers upto Given Limit is :" + ans);
           
        }
    }
}
 

6. Programme generate fibanacii series

6.Define a method to generate fibanacii series.

Code:

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

namespace fibanacii
{
    class Program
    {
        public static void Fib(int n)
        {
            int i, c,a=0,b=1;
            for (i = 0; i <= (n - 3); i++)
            {
                c = a + b;
                a = b;
                b = c;
                Console.WriteLine(+c);
            }
        }
        static void Main(string[] args)
        {
            int n,a = 0, b = 1;
            Console.WriteLine("Enter fibonacci series of nth term : ");
            n = int.Parse(Console.ReadLine());
            Console.WriteLine("a={0},b={1}", a, b);
            Fib(n);
        }
    }
};
 

5. Reverse The Given Number

5.Define a Method to reverse the given number

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

namespace reverse
{
    class Program
    {
        public static int Rev(int num)
        {
            int mod, rev = 0;

            while (num > 0)
            {
                mod = num % 10;
                rev = (rev * 10) + mod;
                num = num / 10;
            }

            return rev;
          
        }
        static void Main(string[] args)
        {
            int num,rev;
            Console.WriteLine("Enter a number:");
            num = int.Parse(Console.ReadLine());
            rev=Rev(num);

            Console.WriteLine("Reverse of the given number:" + rev);
          
        }
    }
}
  

3. Student Program

3.Write a simple program to accept student number,name,marks of 3 subjects. Find the average,total and grade in each subject. And Print the total marks,avg, grade of the student , if he/she got less than 40marks in any subject he/she is failure. 

Code:

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

namespace student
{
    class Program
    {
        static void Main(string[] args)
        {
            int sno, m1, m2, m3;
            double total, avg;
            string sname;
            Console.WriteLine("Enter Student Number");
            sno = int.Parse(Console.ReadLine());
            Console.WriteLine("Enter Student Name");
            sname = Console.ReadLine();
            Console.WriteLine("Enter 3 subject marks of student");
            m1 = int.Parse(Console.ReadLine());
            m2 = int.Parse(Console.ReadLine());
            m3 = int.Parse(Console.ReadLine());
            total = m1 + m2 + m3;
            avg = total / 3;
            Console.WriteLine("grand total :" + total);
            Console.WriteLine("Average Percentage :" + avg);
            if (avg > 60)
            {
                Console.WriteLine("1st class");
            }
            else
            {
                if (avg > 50 && avg < 60)
                {
                    Console.WriteLine("2nd class");
                }
                if (avg > 40 && avg < 50)
                {
                    Console.WriteLine("3rd class");
                }
                else
                    Console.WriteLine("fail");
            }


        }
    }
}
 

1.Finding Quadrant Program

1.Accept Simple Point from the keyboard and check whether it is a origin on x axis , on y axis or in 1st quadrant or in 2nd or in 3rd or in 4th quadrant where exactly the point can be?

Code:

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

namespace quadrant
{
    class Program
    {
        static void Main(string[] args)
        {
            int a,b;
            Console.WriteLine("Please Enter the Point x and y (Ex: 1,2)");
            a=int.Parse(Console.ReadLine());
            b=int.Parse(Console.ReadLine());
            if (a == 0 && b == 0)
            {
                Console.WriteLine("Origin");
            }
            if (a>0 && b>0)
            {
                Console.WriteLine("first quadrant");
            }
            if (a < 0 && b > 0)
            {
                Console.WriteLine("second quadrant");
            }
            if (a < 0 && b < 0)
            {
                Console.WriteLine("third quadrant");
            }
            if (a > 0 && b < 0)
            {
                Console.WriteLine("fourth quadrant");
            }

        }
    }
}
 

4. Sum of Digits Program

4.Write a c# program to find sum of digits in a given number.

Code:

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

namespace sumdigits
{
    class Program
    {
        static void Main(string[] args)
        {
            int n, sum;
            Console.WriteLine("Please enter the number");
            n=int.Parse(Console.ReadLine());
            sum = 0;
            while (n != 0)
            {
                sum += n % 10;
                n /= 10;
            }
            Console.WriteLine("the sum of digits is " + sum);

        }
    }
}

2. Circle Program

2.Write a Simple Program to check whether the given point is on the surface of the circle or inside the circle or outside the circle?

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

namespace circle
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Please Enter the radius of the circle : ");
            int r = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Please Enter the center of the circle ");
            Console.Write("Please Enter x coordinate of the centre : ");
            int x1 = Convert.ToInt32(Console.ReadLine());
            Console.Write("Please Enter y coordinate of the centre : ");
            int y1 = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Please Enter the Point ");
            Console.Write("Please Enter x coordinate of the point : ");
            int x2 = Convert.ToInt32(Console.ReadLine());
            Console.Write("Please Enter y coordinate of the point : ");
            int y2 = Convert.ToInt32(Console.ReadLine());
            int check = Convert.ToInt32(Math.Sqrt(Math.Pow(x2 - x1, 2) + Math.Pow(y2 - y1, 2)));
            if (check < r)
                Console.WriteLine("Inside the Circle");
            else if (check == r)
                Console.WriteLine("On the Circle");
            else
                Console.WriteLine("Outside the Circle");
        }
    }
}