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

A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. To check if a number is prime in C#, we need to verify that it has exactly two divisors.

Algorithm

To determine if a number is prime, we use a for loop to check all numbers from 1 to the given number. We count how many divisors exist by checking if the remainder is zero when dividing the number by each potential divisor −

for (int i = 1; i <= n; i++) {
    if (n % i == 0) {
        count++;
    }
}

If the count equals 2, the number is prime because it's only divisible by 1 and itself.

Using Basic Prime Check

Example

using System;

namespace Demo {
    class MyApplication {
        public static void Main() {
            int n = 5, a = 0;
            for (int i = 1; i <= n; i++) {
                if (n % i == 0) {
                    a++;
                }
            }
            if (a == 2) {
                Console.WriteLine("{0} is a Prime Number", n);
            } else {
                Console.WriteLine("{0} is Not a Prime Number", n);
            }
        }
    }
}

The output of the above code is −

5 is a Prime Number

Using Optimized Prime Check

For better performance, we can optimize by checking divisors only up to the square root of the number −

Example

using System;

class Program {
    public static void Main() {
        int[] numbers = {2, 4, 17, 25, 29};
        
        foreach (int num in numbers) {
            if (IsPrime(num)) {
                Console.WriteLine("{0} is a Prime Number", num);
            } else {
                Console.WriteLine("{0} is Not a Prime Number", num);
            }
        }
    }
    
    static bool IsPrime(int n) {
        if (n <= 1) return false;
        if (n == 2) return true;
        if (n % 2 == 0) return false;
        
        for (int i = 3; i * i <= n; i += 2) {
            if (n % i == 0) {
                return false;
            }
        }
        return true;
    }
}

The output of the above code is −

2 is a Prime Number
4 is Not a Prime Number
17 is a Prime Number
25 is Not a Prime Number
29 is a Prime Number

Using Method with Special Cases

Example

using System;

class PrimeChecker {
    public static void Main() {
        int[] testNumbers = {1, 2, 3, 9, 13, 100};
        
        foreach (int number in testNumbers) {
            Console.WriteLine("{0}: {1}", number, CheckPrime(number) ? "Prime" : "Not Prime");
        }
    }
    
    static bool CheckPrime(int n) {
        if (n <= 1) return false;
        if (n <= 3) return true;
        if (n % 2 == 0 || n % 3 == 0) return false;
        
        for (int i = 5; i * i <= n; i += 6) {
            if (n % i == 0 || n % (i + 2) == 0) {
                return false;
            }
        }
        return true;
    }
}

The output of the above code is −

1: Not Prime
2: Prime
3: Prime
9: Not Prime
13: Prime
100: Not Prime

Comparison of Methods

Method Time Complexity Description
Basic Check O(n) Checks all numbers from 1 to n
Square Root Optimization O(?n) Checks divisors only up to ?n
6k±1 Optimization O(?n) Skips multiples of 2 and 3, checks only 6k±1

Conclusion

Prime number checking in C# can be implemented using various approaches, from basic divisor counting to optimized algorithms. The square root optimization significantly improves performance for large numbers by reducing the number of iterations required.

Updated on: 2026-03-17T07:04:35+05:30

66K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements