C# Program to Check Whether a Number is Prime or Not - C# Program

C# Program to Check Whether a Number is Prime or Not

A Prime Number can be divided evenly only by 1, or itself. And it must be a whole number greater than 1. Example: 5 can only be divided evenly by 1 or 5, so it is prime number

using System;
namespace PrimeNumberApp
{
    class Program
    {
        static void Main(string[] args)
        {
            int num, d;
            bool isPrime = true;
            Console.Write("Enter the Number :");
            num = Convert.ToInt32(Console.ReadLine());
            if(num==1)
                Console.WriteLine("Not a prime number");
            else
            {
                for (d = 2; d <= num/d; d++)
                {
                    if (num % d == 0)
                    {
                         isPrime = false;
                         break;
                    }
                }
                if(isPrime) 
                     Console.WriteLine("Number is a Prime Number");
                else
                     Console.WriteLine("Not a prime number");
            }
                
           

            Console.ReadKey();
        }
    }
}

Output - C# Program to Check Whether a Number is Prime or Not

Enter the Number : 7

Number is a Prime Number

C# Program to Check Whether a Number is Prime or Not