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
C Programming Language Tutorial C is a general-purpose mid-level programming language developed by Dennis M. Ritchie at Bell Laboratories in 1972. It was initially used for the development of UNIX operating system, but it later became popular for a wide range of applications. Today, C remains one of the top three most widely used
5 min read
Dynamic Memory Allocation in C using malloc(), calloc(), free() and realloc() In C, a variable defined in a function is stored in the stack memory. The requirement of this memory is that it needs to know the size of the data to memory at compile time (before the program runs). Also, once defined, we can neither change the size nor completely delete the memory.To resolve this,
9 min read
Data Types in C Each variable in C has an associated data type. It specifies the type of data that the variable can store like integer, character, floating, double, etc.Example:C++int number;The above statement declares a variable with name number that can store integer values.C is a statically type language where
5 min read
C Language Introduction C is a general-purpose procedural programming language initially developed by Dennis Ritchie in 1972 at Bell Laboratories of AT&T Labs. It was mainly created as a system programming language to write the UNIX operating system.Main features of CWhy Learn C?C is considered mother of all programmin
6 min read
C Arrays An array in C is a fixed-size collection of similar data items stored in contiguous memory locations. It can be used to store the collection of primitive data types such as int, char, float, etc., as well as derived and user-defined data types such as pointers, structures, etc. Creating an Array in
7 min read
C Pointers A pointer is a variable that stores the memory address of another variable. Instead of holding a direct value, it has the address where the value is stored in memory. This allows us to manipulate the data stored at a specific memory location without actually using its variable. It is the backbone of
9 min read
C Programs To learn anything effectively, practicing and solving problems is essential. To help you master C programming, we have compiled over 100 C programming examples across various categories, including basic C programs, Fibonacci series, strings, arrays, base conversions, pattern printing, pointers, and
8 min read
Basics of File Handling in C File handling in C is the process in which we create, open, read, write, and close operations on a file. C language provides different functions such as fopen(), fwrite(), fread(), fseek(), fprintf(), etc. to perform input, output, and many different C file operations in our program.Need of File Han
13 min read
Operators in C In C language, operators are symbols that represent some kind of operations to be performed. They are the basic components of the C programming. In this article, we will learn about all the operators in C with examples.What is an Operator in C?A C operator can be defined as the symbol that helps us
11 min read
Bitwise Operators in C In C, bitwise operators are used to perform operations directly on the binary representations of numbers. These operators work by manipulating individual bits (0s and 1s) in a number.The following 6 operators are bitwise operators (also known as bit operators as they work at the bit-level). They are
6 min read