Latest Post

Tuesday, 3 January 2017

How to check whether the entered number is prime or not[C#]

Write a program in C# to check whether the entered number is prime or not.

Following is the simple C# code :
class Lab
    {
        static void Main(string[] args)
        {
            int number;
            Console.Write("Enter a Number : ");
            number = Convert.ToInt32(Console.ReadLine());
            int counter = 0;
            for (int i = 1; i <= number; i++)
            {
                if (number % i == 0)
                {
                    counter++;
                }
            }
            if (counter == 2)
            {
                Console.WriteLine("Entered Number is a Prime Number");
            }
            else
            {
                Console.WriteLine("Not a Prime Number");
            }
            Console.ReadLine();

        }
    }

Output:-
Enter a Number : 12
Not a Prime Number

Enter a Number : 13
Entered Number is a Prime Number


Government Jobs