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

In this C# program, we will take input from the user and check whether the number is prime or not. A prime number is a positive integer that is divisible only by 1 and itself.

 

 private static void Main(string[] args)
        {
            Console.ForegroundColor= ConsoleColor.Yellow;
            int num;
            bool Is_Prime_Number = true;
            Console.WriteLine("Enter the Number to check Prime: ");
            num = int.Parse(Console.ReadLine());
            for (int i = 2; i <= num / 2; i++)
            {
                if (num % i == 0)
                {
                    Console.WriteLine("Number is not Prime.");
                    Is_Prime_Number = false;
                    break;
                }
            }
            if (Is_Prime_Number == true)
            Console.WriteLine("Number is Prime.");
            Console.ReadKey();
        }

 

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

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

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

 
We are nothing without our users ! You can help us offer even more high quality content. Please share our page !
Best quality Asp .Net Ajax Control Toolkit tutorials.

Give your valuable comments.

Name
Email
Comment
5 + 2 =
 
About Us | Terms of Use | Privacy Policy | Disclaimer | Contact Us Copyright © 2012-2022 CodingFusion
75 Important SQL Server queries you should know