Question no. 1 – Write a program to print “Hello World” in C#

using System;  
public class Program {
	public static void Main(string[] args)
    {
        Console.WriteLine("Hello World");  
    }
}

// ------------------------------------------
// output
Hello World

Question no. 2 – Write a program to check if a given number is prime or nor?, in ASP.NET using C#

using System;

public class Program
{
  public static bool isPrime(int n)
    {
        if (n == 0 || n == 1)
        {
            return false;
        }
        for (int i = 2; i <= n/2; i++)
        {
            if (n % i == 0)
            {
                return false;
            }
        }
        return true;
    }
  public static void Main(string[] args)
    {
        Console.WriteLine("Enter a number: "); 
        int n = Console.Read();
        if (isPrime(n))
        {
            Console.WriteLine("Number is prime");
        }
        else
        {
            Console.WriteLine("Number is not prime");
        }
    }
}
// ----------------------------------------------
// output
Enter a number:
5
Number is prime

Question no. 3 – Write a program to display the prime numbers between a given range of numbers in C#

using System; 

public class Program
{
    public static bool isPrime(int n)
    {
        if (n == 0 || n == 1)
        {
            return false;
        }
        for (int i = 2; i <= n/2; i++)
        {
            if (n % i == 0)
            {
                return false;
            }
        }
        return true;
    }
  
    public static void PrintPrimeInRange(int start, int end)
    {
        for (int i = start; i <= end; i++)
        {
            if (isPrime(i))
            {
                Console.Write(i);
              	Console.Write("\n");
            }
        }
    }
  
    public static void Main(string[] args)
    { 
        int a, b;
        Console.WriteLine("Enter range: "); 
        a = Convert.ToInt32(Console.ReadLine()); 
        b = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("Prime number between given range");
        PrintPrimeInRange(2, 10); 
    }
}
// ..............................................................
output
Enter range:
2
10
Prime number between given range
2
3
5
7

Question no. 4- Write a program to calculate the area of a triangle.

using System; 

public class Program
{
	public static float TriArea(int b, int h)
    {
        return (float)(0.5 * (b * h));
    }
	public static void Main(string[] args)
    {
        Console.WriteLine("Enter breadth and height of triangle: ");
        int breadth = Convert.ToInt32(Console.ReadLine());
        int height = Convert.ToInt32(Console.ReadLine());
        float ans = TriArea(breadth, height);
        Console.WriteLine("Area of triangle is ");
        Console.WriteLine(ans);
    }
}

// ..................................................
// output
Enter breadth and height of triangle:
3
6
Area of triamgle is
9

Question no. 5- Write a program to calculate the area of a square.

using System;

public class Program
{
	public static void Main(string[] args)
    {
        Console.WriteLine("Enter a side of square: ");
        int side = Convert.ToInt32(Console.ReadLine()); 
        float ans = side*side;
        Console.WriteLine("Area of square is ");
        Console.WriteLine(ans);
    }
}

// ..................................................
//output
Enter a side of square: 
3
Area of square is
9

Question no. 6- Write a program to convert Fahrenheit to Celsius in C#.

using System;

public class Program
	{    
      public static float FtoC(int f)
      {
          int celsius = (f - 32) * 5 / 9;
          return celsius;
      }    
      public static void Main(string[] args)
      {
          Console.WriteLine("Enter temparature in Fahrenheit ");
          int t = Convert.ToInt32(Console.ReadLine());
          float ans = FtoC(t);
          Console.WriteLine("Celsius ");
          Console.WriteLine(ans);
      }
}

// ..................................................
// output
Enter temperature in Fahrenheit
100
Celsius
37

Question no. 7- Write a program to print the factorial of a number using C#.

    
    
using System; 
public class Program
{ 
	public static int fact(int n)
    {
        if (n == 0)
        {
            return 1;
        }
        return n * fact(n - 1);
    }
	public static void Main(string[] args)
    {
        Console.WriteLine("Enter a number ");
        int n = Convert.ToInt32(Console.ReadLine());
        float ans = fact(n);
        Console.WriteLine("factorial is ");
        Console.WriteLine(ans);
    }
}

// ..................................................
// output
Enter a number
5
factorial is
120

Question no. 8- Write a program to print right-angle triangle in star pattern in C#.

using System; 
public class Program
{
    public static void angleTri(int n)
    {
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j <= i; j++)
            {
                if (j == 0 || i == n - 1 || i == 0 || j == i)
                    Console.Write("*");
                else
                    Console.Write(" ");

            }
            Console.WriteLine();
        }
    }
    public static void Main(string[] args)
    {
      Console.WriteLine("Enter a number ");
      int n = Convert.ToInt32(Console.ReadLine());
      angleTri(n);
    }
}

// ..................................................
// output
Enter a number
8
*
**
* *
*  *
*   *
*    *
*     *
********

Question no. 9- Write a program to calculate the sum of digits of a number, using C#.

    
using System;

public class Program
{
    public static int digitSum(int n)
    {
        int s = 0;
        while (n != 0)
        {
            int r = n % 10;
            s = s + r;
            n = n / 10;
        }
        return s;
    }

	public static void Main(string[] args)
    {
        Console.WriteLine("Enter a number ");
        int n = Convert.ToInt32(Console.ReadLine());
        int ans = digitSum(n);
        Console.WriteLine("Digit sum: ");
        Console.Write(ans);
    }
}

// ..................................................
// output
Enter a number
1234
Digit sum:
10

Question no. 10- Write a program to calculate interest using C#.

    
using System; 
public class Program
{ 
    public static float interest(int p, int r, int t)
    {
        return (p * r * t)/100;
    }

	public static void Main(string[] args)
    {
        Console.WriteLine("Enter amount, rate and time ");
        
        int p = Convert.ToInt32(Console.ReadLine());
        int r = Convert.ToInt32(Console.ReadLine());
        int t = Convert.ToInt32(Console.ReadLine());
        float ans = interest(p, r, t);
        Console.WriteLine("Interest is : ");
        Console.Write(ans);
    }
}

// ..................................................
// output
Enter amount, rate and time
1000
5
2
Interest is :
100

ये सभी दिए गए programs Visual Studio IDE पर success run किया गए है, यदि समस्या आती है तो आप वे झिझक हमें comment box में बता सकते में कोशिश करूँगा जल्द से जल्द ठीक कर दू।

यदि आपको कोई और programs चाहिए है तो आप हमें comment में बता सकते है (जो आपकी परीक्षा की दृष्टी से important हो या प्रैक्टिकल आदि में पूछे गए हो), जल्द-से-जल्द upload करने की कोशिश करूँगा, हमारा प्रयास अच्छा लगा हो तो आपने Friends & College (WhatsApp) आदि पर शेयर कर सकते है।


4 responses to “C# Important Programs”

  1. Amisha Avatar
    Amisha

    awesome 👍

  2.  Avatar
    Anonymous

    🙂

  3.  Avatar
    Anonymous

    Thankyou so much

  4.  Avatar
    Anonymous

    hi

Leave a Reply

Your email address will not be published. Required fields are marked *


4 thoughts on “C# Important Programs”

Leave a Reply

Your email address will not be published. Required fields are marked *