Write a C# Program to check whether a number is prime or not

How to write a C# program to find prime numbers. What is a prime number? And how to find the prime number between two numbers in C#.

At the very first let’s understand what is a prime number? Then will write the code to find prime number in C#. List of C# programs with examples.

What is a prime number?

A number that is divisible only by itself and 1. Example: (2, 3, 5, 7, 11, 13, 17, 19). One is not a prime number according to the definition a Prime number is divisible with 1 and itself and one doesn’t have exactly two positive divisors.

C# Program to check whether a number is prime or not

In this example, We will see how to check whether a number is prime or not in c#. When user will run this code It will ask to enter a number. The output will print the number is prime or not.

using System;
 
namespace CSharpProgram
{
    class Program
    {
        static void Main(string[] args)
        {
            int num, i, f = 0;
            Console.Write("Enter any Number: ");
            num = int.Parse(Console.ReadLine());
          
            for (i = 2; i <= num/2; i++)
            {
                if (num % i == 0)
                {
                    Console.Write("No "+num+" is not a Prime Number");
                    f = 1;
                    break;
                }
            }
            if (f == 0) {
                Console.Write("Yes " +num+ " is a Prime Number");
            }
 
            Console.ReadLine();
        }
    }
}

More examples to find the prime number in C#

Below, We can see more C# program to find prime number between two numbers, for example, find a prime number between 1 to 100 in C#.

Write a C# program to find Prime Number between 1-100

using System;
 
namespace CSharpProgram
{
    class Program
    {
        static void Main(string[] args)
        {
            bool isPrime = true;
            Console.WriteLine("Prime Numbers between 1 to 100 : ");
            for (int i = 2; i <= 100; i++)
            {
                for (int j = 2; j <= 100; j++)
                {
                    if (i != j && i % j == 0)
                    {
                        isPrime = false;
                        break;
                    }
 
                }
                if (isPrime)
                {
                    Console.Write(" " + i);
                }
                isPrime = true;
            }
            Console.ReadKey();
        }
    }
}

Write a C# program to find Prime Number between n-N.

using System;
 
namespace CSharpProgram
{
    class Program
    {
        static void Main(string[] args)
        {
            int n1 = int.Parse(Console.ReadLine());
            int n2 = int.Parse(Console.ReadLine());
            bool isPrime = true;
            Console.WriteLine("Prime Numbers between "+n1+" to "+n2+" : ");
            for (int i = n1; i <= n2; i++)
            {
                for (int j = 2; j <= n2; j++)
                {
                    if (i != j && i % j == 0)
                    {
                        isPrime = false;
                        break;
                    }
 
                }
                if (isPrime)
                {
                    Console.Write(" " + i);
                }
                isPrime = true;
            }
            Console.ReadKey();
        }
    }
}

 

Leave a Reply

Your email address will not be published.

seventeen − twelve =