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

 
Best quality Asp .Net Ajax Control Toolkit tutorials.

Give your valuable comments.

Name
Email
Comment
8 + 6 =
 

About Us | Terms of Use | Privacy Policy | Disclaimer | Contact Us Copyright © 2012-2024 CodingFusion
50+ C# Programs for beginners to practice