C Program to Display Prime Numbers Between Intervals
Last Updated :
12 May, 2023

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
OutputPrime 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:
- Create a boolean array prime[srt to n] and initialize all its entries as true.
- Mark prime[0] and prime[1] as false because they are not prime numbers.
- 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.
- 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
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.
Similar Reads
Program to find Prime Numbers Between given Interval Given two numbers m and n as interval range, the task is to find the prime numbers in between this interval.Examples: Input: m = 1, n = 10Output: 2 3 5 7Input : m = 10, n = 20Output : 11 13 17 19Table of Content[Naive Approach] Basic Trial Division Method - O(n*n) Time and O(1) Space[Optimised Appro
15+ min read
Program to find Prime Numbers Between given Interval Given two numbers m and n as interval range, the task is to find the prime numbers in between this interval.Examples: Input: m = 1, n = 10Output: 2 3 5 7Input : m = 10, n = 20Output : 11 13 17 19Table of Content[Naive Approach] Basic Trial Division Method - O(n*n) Time and O(1) Space[Optimised Appro
15+ min read
Program to find Prime Numbers Between given Interval Given two numbers m and n as interval range, the task is to find the prime numbers in between this interval.Examples: Input: m = 1, n = 10Output: 2 3 5 7Input : m = 10, n = 20Output : 11 13 17 19Table of Content[Naive Approach] Basic Trial Division Method - O(n*n) Time and O(1) Space[Optimised Appro
15+ min read
C Program to Display Prime Numbers Between Two Intervals Using Functions Prime numbers have only 2 factors, 1 and themselves. For example, 2,3, 5, 7, 9,... are the first 5 prime numbers. Here we will build a C program to display prime numbers between two intervals using functions using 2 approaches, for loop and while loop. Example Input: num1 = 2, num2 = 10 Output: Prim
3 min read
C Program to Display Armstrong Number Between Two Intervals Write a C program to find and display all Armstrong numbers between two given intervals. An Armstrong number (also known as a narcissistic number or pluperfect number) of a given number of digits is a number that is equal to the sum of its own digits each raised to the power of the number of digits.
6 min read
C Program To Find Prime Numbers Between Given Range A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. For example, 2, 3, 5, 7, and 11 are prime numbers. In this article, we will learn how to find all the prime numbers between the given range.ExampleInput: l = 10, r = 30Output: 11 13 17 19Explanat
6 min read