Skip to content
Home » Programming » C# Program Examples » C# Program to check whether the number is prime or not.

C# Program to check whether the number is prime or not.

C# Program to check whether the number is prime or not.

In this tutorial, we’ll be doing a C# Program to check whether the given numer is Prime or Not.
The Program below is to check whether the number is Prime Number or not.

//C# Program to check whether the number given by user is Prime Number or not.
using System;

namespace CheckPrime
{
    class Program
    {
        static void Main(string[] args)
        {
            int num, j, t = 0, flag = 0; //initialzation of interger variables
            Console.WriteLine("Please enter number to check: ");
            num = int.Parse(Console.ReadLine()); //accepting integer value from the user
            t = num / 2;
            for (j = 2; j <= t; j++)
            {
                if (num % j == 0)
                {
                    Console.WriteLine("Not Prime Number.");
                    flag = 1;
                    break;
                }
            }
            if (flag == 0)
                Console.WriteLine("Prime Number.");
        }
    }
}

Output:

Please enter number to check:
5
Prime Number.
output of check whether the number is prime or not in c#
Output

Explaination:

We’ve already discussed about using System;, namespace & class Program. So, lets go to the main Program directly.

int num, j, t = 0, flag = 0;

Here, num, j, t, and flag are integer variables declared initially. The initial value of t and flag is set to 0. Variable j is used as loop counter. Similary, num is use for storing the number entered by the user.

Console.WriteLine("Please enter number to check: ");

This line simply displays a Message written inside the quotation box. Here, we’ve writeen a message for the user to enter a number to check whether it is Prime or not.

num = int.Parse(Console.ReadLine());

This line just accept the number (intger value) entered by the user and store it in the inter variable num.

t = num / 2;
for (j = 2; j <= t; j++)
{
     if (num % j == 0)
    {
           Console.WriteLine("Not Prime Number.");
           flag = 1;
           break;
    }
}
if (flag == 0)
Console.WriteLine("Prime Number.");

Prime Numbers are those numbers which are exactly divisible by 1 & itself only.
Basically the above block of codes checks whether the number is exactly divisible by any other number except 1 and the number itself.
If the number is exactly divisible by any other number then the value of flag changes to 1 & gives output as “Not Prime Number”.
Else the value of flag remains 0 & gives output as “Prime Number”.


In this way, the C# program to check wether the number is prime or not.

Also Read: C# Hello World
C Sharp for Beginners

2 thoughts on “C# Program to check whether the number is prime or not.”

    1. Flag is just a variable that holds the value 0 and 1 like true or false.
      Initially, we initialize the value of flag as 0. That means, we consider any number given by the user as prime number.
      As the programs goes on, inside the loop, we check the if the number is divisible by any other numbers except 1 and itself. If it is, then we change the value of flag to 1.
      Then outside the loop, again we check the value of flag. If the value of flag is 0 then the number is prime and if the value of number is 1 then the number is not prime.

Leave a Reply

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