C# Program to Find Prime Numbers Within a Range

In this article, we are going to discuss how we can find prime numbers present within a given range using C#.

What are Prime Numbers?

Prime numbers are natural numbers greater than 1 that have exactly two factors 1 and the number itself. They have no other positive divisors. Examples of prime numbers are 2, 3, 5, 7, 11, 13, 17, etc.

Key Facts:

  • The smallest prime number is 2, which is also the only even prime number.

  • The number 1 is not a prime number because it has only one factor (itself).

  • All prime numbers except 2 are odd numbers.

Prime vs Composite Numbers Prime Numbers Only 2 factors: 1 and itself 2, 3, 5, 7, 11... Composite Numbers More than 2 factors Can be divided by 4, 6, 8, 9, 10...

Problem Description

Given two numbers representing the starting and ending values of a range, we need to find all prime numbers within that range (inclusive).

Examples

Input: start = 30, end = 50
Output: 31 37 41 43 47
Explanation: These numbers between 30 and 50 have only two factors 1 and themselves.

Input: start = 160, end = 190
Output: 163 167 173 179 181
Explanation: These are all the prime numbers in the given range.

Using Basic Prime Check

The basic approach checks each number in the range by testing divisibility from 2 up to the square root of the number. If no divisor is found, the number is prime.

Algorithm Steps

  • For each number in the range, check if it's less than or equal to 1 (not prime).

  • Test divisibility from 2 to ?number using a loop.

  • If no divisor is found, the number is prime.

using System;

class Program {
    static bool IsPrime(int number) {
        if (number <= 1)
            return false;
        
        for (int i = 2; i * i <= number; i++) {
            if (number % i == 0)
                return false;
        }
        return true;
    }
    
    static void FindPrimesInRange(int start, int end) {
        Console.WriteLine($"Prime numbers between {start} and {end}:");
        
        for (int i = start; i <= end; i++) {
            if (IsPrime(i)) {
                Console.Write(i + " ");
            }
        }
        Console.WriteLine();
    }
    
    static void Main() {
        FindPrimesInRange(30, 50);
        FindPrimesInRange(160, 190);
    }
}

The output of the above code is

Prime numbers between 30 and 50:
31 37 41 43 47 
Prime numbers between 160 and 190:
163 167 173 179 181 

Using Optimized Prime Check

The optimized approach handles special cases and skips even numbers after 2, reducing unnecessary checks by approximately half.

using System;

class Program {
    static bool IsPrimeOptimized(int number) {
        if (number <= 1)
            return false;
        if (number == 2)
            return true;
        if (number % 2 == 0)
            return false;
        
        // Check only odd divisors from 3 to ?number
        for (int i = 3; i * i <= number; i += 2) {
            if (number % i == 0)
                return false;
        }
        return true;
    }
    
    static void FindPrimesOptimized(int start, int end) {
        Console.WriteLine($"Optimized prime search from {start} to {end}:");
        
        for (int i = start; i <= end; i++) {
            if (IsPrimeOptimized(i)) {
                Console.Write(i + " ");
            }
        }
        Console.WriteLine();
    }
    
    static void Main() {
        FindPrimesOptimized(900, 920);
        FindPrimesOptimized(100, 120);
    }
}

The output of the above code is

Optimized prime search from 900 to 920:
907 911 919 
Optimized prime search from 100 to 120:
101 103 107 109 113 

Performance Comparison

Approach Time Complexity Space Complexity Optimization
Basic Prime Check O((end?start) × ?n) O(1) Checks all divisors up to ?n
Optimized Prime Check O((end?start) × ?n/2) O(1) Skips even divisors, ~2x faster

How It Works

Both approaches use the mathematical principle that if a number n has a divisor greater than ?n, it must also have a corresponding divisor less than ?n. Therefore, we only need to check divisors up to ?n to determine if a number is prime.

The optimized version further reduces checks by:

  • Handling 2 as a special case (the only even prime)

  • Immediately returning false for all other even numbers

  • Testing only odd divisors starting from 3

Conclusion

Finding prime numbers within a range can be efficiently accomplished using optimized primality testing. The key optimization is checking divisors only up to the square root and skipping even numbers, which significantly reduces the number of operations required for large ranges.

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

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements