Open In App

C Program to Display Prime Numbers Between Intervals

Last Updated : 12 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

C Program to Display Prime Numbers Between Intervals

Given two numbers a and b as interval range, the task is to find the prime numbers in between this interval.

Examples: 

Input: a = 1, b = 10
Output: 2, 3, 5, 7
Input: a = 10, b = 20
Output: 11, 13, 17, 19

Approach 1:

In the below program, the range of numbers is taken as input and stored in the variables ‘a’ and ‘b’. Then using for-loop, the numbers between the interval of a and b are traversed. For each number in the for loop, it is checked if this number is prime or not. If found prime, print the number. Then the next number in the loop is checked, till all numbers are checked.

Program: 

C




// C Program to Display Prime
// Numbers Between Intervals
#include <stdio.h>
 
// Driver code
int main()
{
    // Declare the variables
    int a, b, i, j, flag;
 
    // Ask user to enter lower value
    // of interval
    printf("Enter lower bound of the interval: ");
 
    // Take input
    scanf("%d", &a);
 
    // Ask user to enter upper value
    // of interval
    printf("Enter upper bound of the interval: ");
 
    // Take input
    scanf("%d", &b);
 
    // Print display message
    printf("Prime numbers between %d and %d are: ",
            a, b);
 
    // Traverse each number in the interval
    // with the help of for loop
    for (i = a; i <= b; i++)
    {
        // Skip 0 and 1 as they are
        // neither prime nor composite
        if (i == 1 || i == 0)
            continue;
 
        // flag variable to tell
        // if i is prime or not
        flag = 1;
 
        for (j = 2; j <= i / 2; ++j)
        {
            if (i % j == 0)
            {
                flag = 0;
                break;
            }
        }
 
        // flag = 1 means i is prime
        // and flag = 0 means i is
        // not prime
        if (flag == 1)
            printf("%d ", i);
    }
 
    return 0;
}


Output: 

Enter lower bound of the interval: 1
Enter upper bound of the interval: 10
Prime numbers between 1 and 10 are: 2 3 5 7 

Approach 2:

If a number ‘n’ is not divided by any number less than or equals to the square root of n then, it will not be divided by any other number greater than the square root of n. So, we only need to check up to the square root of n.

Below is the implementation of the above approach:

C




// C Program to Display Prime
// Numbers Between Intervals
#include <stdbool.h>
#include <stdio.h>
 
// function to check if a given number is prime
bool isPrime(int n)
{
    // since 0 and 1 is not prime return false.
    if (n == 1 || n == 0)
        return false;
 
    // Run a loop from 2 to square root of n.
    for (int i = 2; i * i <= n; i++) {
        // if the number is divisible by i, then n is not a
        // prime number.
        if (n % i == 0)
            return false;
    }
    // otherwise, n is prime number.
    return true;
}
 
// Driver code
int main()
{
    // lower value of interval
    int a = 1;
 
    // upper value of interval
    int b = 10;
 
    // Print display message
    printf("Prime numbers between %d and %d are: ", a, b);
 
    // Traverse each number in the interval
    // with the help of for loop
    for (int i = a; i <= b; i++) {
        // If the number is prime print it
        if (isPrime(i))
            printf("%d ", i);
    }
 
    return 0;
}
 
// This code is contributed by Susobhan Akhuli


Output

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

Time Complexity: O(N*sqrt(N)), where max value of N is b.
Auxiliary Space: O(1), since no extra space has been taken.

Approach 3: (Using Sieve of Eratosthenes Method)

The sieve of Eratosthenes is one of the most efficient ways to find all primes smaller than N when N is smaller than 10 million or so.

Steps:

  1. Create a boolean array prime[srt to n] and initialize all its entries as true.
  2. Mark prime[0] and prime[1] as false because they are not prime numbers.
  3. Starting from p = srt, for each prime number p less than or equal to the square root of n, mark all multiples of p greater than or equal to p*p as composite by setting prime[i] to false.
  4. Finally, print all prime numbers between srt and n.

Below is the implementation of the above approach:

C




// C program to find the prime numbers
// between a given interval using Sieve of Eratosthenes
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
 
void SieveOfEratosthenes(int srt, int n)
{
    // Create a boolean array "prime[srt to n]" and
    // initialize all entries it as true. A value in
    // prime[i] will finally be false if i is Not a prime,
    // else true.
    bool prime[n + 1];
    memset(prime, true, sizeof(prime));
    prime[0] = false;
    prime[1] = false;
 
    for (int p = 2; p * p <= n; p++) {
        // If prime[p] is not changed, then it is a prime
        if (prime[p] == true) {
            // Update all multiples of p greater than or
            // equal to the square of it numbers which are
            // multiple of p and are less than p^2 are
            // already been marked.
            for (int i = p * p; i <= n; i += p)
                prime[i] = false;
        }
    }
 
    // Print all prime numbers
    for (int p = srt; p <= n; p++)
        if (prime[p])
            printf("%d ", p);
}
 
// Driver Code
int main()
{
    int srt = 1;
    int end = 10;
    SieveOfEratosthenes(srt, end);
    return 0;
}
 
// This code is contributed by Susobhan Akhuli


Output

2 3 5 7 

Complexity Analysis:

Time Complexity:
The time complexity of the Sieve of Eratosthenes algorithm is O(n*log(log(n))) as it iterates over all numbers from 2 to n and removes all the multiples of each prime number.

Auxiliary Space:
The space complexity of the algorithm is O(n), which is the size of the boolean array prime.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads