C Program to Display Prime Numbers Between Intervals

Last Updated : 5 Sep, 2026

A prime number is a positive integer greater than 1 that has exactly two factors: 1 and itself. Given two limits, the task is to find and display all prime numbers within the given range.

Examples

Input: a = 1, b = 10
Output: 2, 3, 5, 7

Input: a = 10, b = 20
Output: 11, 13, 17, 19

Prime-Numbers-in-Interval-1024x512

Approaches to Display Prime Numbers Between Intervals

We can find prime numbers within a given range using the following approaches:

1. Basic Iterative Approach

For each number in the given range, check whether it is divisible by any number from 2 to n - 1. If it is not divisible by any of them, it is prime.

C
#include <stdio.h>

// Checks whether a number is prime
int isPrime(int n)
{
    // Numbers less than 2 are not prime
    if (n < 2)
        return 0;

    // Check if n is divisible by any number from 2 to n - 1
    for (int i = 2; i < n; i++) {
        if (n % i == 0)
            return 0;  // n has a divisor, so it is not prime
    }

    return 1;  // n has no divisor, so it is prime
}

int main()
{
    int a = 1, b = 10;

    printf("Prime numbers between %d and %d are:\n", a, b);

    // Check every number in the given range
    for (int i = a; i <= b; i++) {
        if (isPrime(i))
            printf("%d ", i);
    }

    return 0;
}

Output
Prime numbers between 1 and 10 are:
2 3 5 7 

Explanation

  • isPrime() checks whether a number has any divisor other than 1 and itself.
  • The for loop checks every number from a to b.
  • Prime numbers are printed as they are found.

2. Optimized Approach Using Square Root

A number cannot have a factor greater than its square root without having a corresponding factor smaller than the square root. Therefore, we only need to check divisors up to √n.

C
#include <stdio.h>

// Checks whether a number is prime
int isPrime(int n)
{
    // Numbers less than 2 are not prime
    if (n < 2)
        return 0;

    // Check divisors only up to the square root of n
    for (int i = 2; i * i <= n; i++) {
        if (n % i == 0)
            return 0;  // n has a divisor, so it is not prime
    }

    return 1;  // n has no divisor, so it is prime
}

int main()
{
    int a = 1, b = 10;

    printf("Prime numbers between %d and %d are:\n", a, b);

    // Check every number in the given range
    for (int i = a; i <= b; i++) {
        if (isPrime(i))
            printf("%d ", i);
    }

    return 0;
}

Output
Prime numbers between 1 and 10 are:
2 3 5 7 

Explanation

  • isPrime() checks divisibility only up to √n using i * i <= n.
  • This reduces the number of checks required for each number.
  • The outer loop prints all prime numbers in the given range.

3. Sieve of Eratosthenes

The Sieve of Eratosthenes finds all prime numbers up to a given limit by repeatedly marking the multiples of each prime number as composite.

Steps

  • Create a boolean array and initially mark all numbers as prime.
  • Mark 0 and 1 as non-prime.
  • Starting from 2, mark all multiples of each prime number as non-prime.
  • Print the numbers that remain marked as prime within the given range.
C
#include <stdio.h>
#include <stdbool.h>

// Finds and prints prime numbers in the given range
void sieve(int low, int high)
{
    bool prime[high + 1];

    // Assume all numbers are prime initially
    for (int i = 0; i <= high; i++)
        prime[i] = true;

    // 0 and 1 are not prime
    prime[0] = prime[1] = false;

    // Mark multiples of each prime as non-prime
    for (int i = 2; i * i <= high; i++) {
        if (prime[i]) {
            for (int j = i * i; j <= high; j += i)
                prime[j] = false;
        }
    }

    // Print prime numbers within the given range
    for (int i = low; i <= high; i++) {
        if (prime[i])
            printf("%d ", i);
    }
}

int main()
{
    int low = 1, high = 10;

    printf("Prime numbers between %d and %d are:\n",
           low, high);

    sieve(low, high);

    return 0;
}

Output
Prime numbers between 1 and 10 are:
2 3 5 7 

Explanation

  • prime[i] stores whether i is prime.
  • For every prime number, its multiples starting from i * i are marked as non-prime.
  • Finally, the remaining prime numbers within the given range are printed.
Comment