Exclusive Clothing Sale On Flipkart

Get up to 50% off on our latest collection.

Shop Now

Prime Number in C#

 

Prime Number in C#

A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself. 

static void Main(string[] args)

        {

            Console.WriteLine("Enter a number");

            int number = Convert.ToInt32(Console.ReadLine());

            int result = Check_Prime(number);

            if (result == 0)

            {

                Console.WriteLine("{0} is not a prime number", number);

            }

            else

            {

                Console.WriteLine("{0} is  a prime number", number);

            }

            Console.Read();

        }

 

        private static int Check_Prime(int number)

        {

            int i;

            for (i = 2; i <= number - 1; i++)

            {

                if (number % i == 0)

                {

                    return 0;

                }

            }

            if (i == number)

            {

                return 1;

            }

            return 0;

        }


Output











Share this

Related Posts

Previous
Next Post »

Exclusive Clothing Sale On Flipkart

Get up to 50% off on our latest collection.

Shop Now