Write a C# Program to check whether a number is prime or not

How to write a C# program to find prime numbers. What is a prime number? And how to find the prime number between two numbers in C#.

At the very first let’s understand what is a prime number? Then will write the code to find prime number in C#. List of C# programs with examples.

What is a prime number?

A number that is divisible only by itself and 1. Example: (2, 3, 5, 7, 11, 13, 17, 19). One is not a prime number according to the definition a Prime number is divisible with 1 and itself and one doesn’t have exactly two positive divisors or we can say here a prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself.

C# Program to check whether a number is prime or not

In this example, We will see how to check whether a number is prime or not in c#. When user will run this code It will ask to enter a number. The output will print the number is prime or not.

using System;
 
namespace CSharpProgram
{
    class Program
    {
        static void Main(string[] args)
        {
            int num, i, f = 0;
            Console.Write("Enter a Number: ");
            num = int.Parse(Console.ReadLine());
          
            for (int i = 1; i <= num; i++)
         {
            if (num % i == 0)
            {
               f++;
            }
         }
            if (f == 2) 
            {
            Console.WriteLine("Number is Prime.");
            } 
            else 
            {
          Console.WriteLine("Number is Not Prime.");
            }
 
            Console.ReadLine();
        }
    }
}

For calculating whether a number is prime or not, we have used a for loop in every iteration of the loop, it will check the condition that  variable i is smaller than or equal to the input number if this condition true than the control goes to first if statement  which find that the remainder is equal to 0,  between i and the number itself.

for (int i = 1; i <= num; i++) 
{
   if (num % i == 0) 
   {
      f++;
   }
}

If it’s true then it increment the counter f value by 1, If this condition is false then the control goes to next if condition.Which check the condition if a counter f is = 2 so it will display the output that  “Number is Prime.” else it will display “Number is Not Prime.”.

Output screen:

More examples to find the prime number in C#

Below, We can see more C# program to find prime number between two numbers, for example, find a prime number between 1 to 100 in C#.

Write a C# program to find Prime Number between 1-100

using System;
 
namespace CSharpProgram
{
    class Program
    {
        static void Main(string[] args)
        {
            bool isPrime = true;
            Console.WriteLine("Prime Numbers between 1 and 100 are : ");                                  
    Console.WriteLine("=========================");
    for (int i = 2; i <= 100; i++)
            {
                for (int j = 2; j <= 100; j++)
                {
                    if (i != j && i % j == 0)
                    {
                        isPrime = false;
                        break;
                    }
 
                }
                if (isPrime)
                {
                    Console.Write(" " + i);
                }
                isPrime = true;
            }
            Console.ReadKey();
        }
    }
}
Output screen:

Write a C# program to find Prime Number between any range / two numbers n1 & n2.

using System;
 
namespace CSharpProgram
{
    class Program
    {
        static void Main(string[] args)
        {
            int n1 = int.Parse(Console.ReadLine());
            int n2 = int.Parse(Console.ReadLine());
            bool isPrime = true;
            Console.WriteLine("Prime Numbers between "+n1+" to "+n2+" : ");
            for (int i = n1; i <= n2; i++)
            {
                for (int j = 2; j <= n2; j++)
                {
                    if (i != j && i % j == 0)
                    {
                        isPrime = false;
                        break;
                    }
 
                }
                if (isPrime)
                {
                    Console.Write(" " + i);
                }
                isPrime = true;
            }
            Console.ReadKey();
        }
    }
}