C# program to check whether the given number is Prime number or not

Prime Number:-Prime number is number that is greater than 1,Prime Number is divisible by 1 or number itself,Prime number has only two factor 1 or number itself.for example 2,3,5,7,11,13 and 17 are prime numbers. number.2 is smallest prime number. 2 is only even prime number.


Algorithm:-

(1)Take number n from user and variable a with initial value zero

(2)divide these number from 1 to n

(3)if number n is divisible only by 1 and n  then increment value of variable 2 times

(4)if increment value of a will equal to 2 then number is prime number else number is not prime number.


using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;


namespace TestApplicationOnly

{

    class Program

    {

        static void Main(string[] args)

        {

            try

            {

                int n, a = 0;

                Console.Write("Enter the number :");

                n = Convert.ToInt32(Console.ReadLine());

                for (int i = 1; i <= n; i++)

                {

                    if (n % i == 0)

                    {

                        a++;

                    }

                }

                if (a == 2)

                {

                    Console.WriteLine(n+" is a Prime Number");

                }

                else

                {

                    Console.WriteLine(n+" is not a Prime Number");

                }          

            }

            catch (Exception ex)

            {

                Console.WriteLine("Input is not in correct formate");

            }

            Console.ReadKey();           

        }

    }

}


Output:-

Enter the number :5

5 is a Prime Number

Enter the number :6

6 is not a Prime Number

एक टिप्पणी भेजें

0 टिप्पणियाँ