C Program to Implement Sieve of Eratosthenes
Last Updated :
23 Jul, 2025
The Sieve of Eratosthenes is a simple and efficient algorithm for finding all prime numbers up to a specified integer. It was created by the ancient Greek mathematician Eratosthenes and is still used today due to its efficiency when dealing with large numbers.
In this article, we will learn how to implement sieve of Eratosthenes in C.
Implementation of the Sieve of Eratosthenes in C
To get started, we go through each number from 2 onwards marking off its multiples as composite numbers (not prime). This is repeated for every prime found.
Algorithm
- To start, make a list of prime[] having n+1 indices with each having true, where true represents the index has a prime number.
- Since 0 and 1 are not prime numbers, set prime[0] and prime[1] to false.
- Starting from 2, for i=2, is i contained in prime[]? If yes, then proceed: true means it is a prime number otherwise it is not so skip all other steps that follows and move to the next i. After that go through all multiples of i (like i*i, i*i+i, i*i+2i,...) such that each multiple is less than or equal to n and make them false in the list.
- Similarly find all multiples which are less than or equal to n and set them false in prime[] array.
- Do the above process until you reach sqrt(n). Any index that is still marked as true in the list, shows that number is a prime one.
The above method is not only easy but it also works very efficiently especially when you want to find all prime numbers below any given maximum limit
C Program to Implement Sieve of Eratosthenes
Below is an example of a C program implementing the sieve of Eratosthenes:
C
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
// Define SieveOfEratosthenes function
void SieveOfEratosthenes(int n)
{
// Allocate memory for prime array and initialize all
// elements as true
bool* prime = malloc((n + 1) * sizeof(bool));
for (int i = 0; i <= n; i++)
prime[i] = true;
// 0 and 1 are not prime numbers
prime[0] = prime[1] = false;
// For each number from 2 to sqrt(n)
for (int p = 2; p <= sqrt(n); p++) {
// If p is prime
if (prime[p]) {
// Mark all multiples of p as non-prime
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
}
// Print all prime numbers up to n
printf("Prime numbers up to %d:\n", n);
for (int p = 2; p <= n; p++) {
if (prime[p])
printf("%d ", p);
}
printf("\n");
// Free allocated memory
free(prime);
}
// Define main function
int main()
{
// Declare variable to hold maximum number
int n;
// Prompt user to enter the maximum number
printf("Enter the maximum number to find primes: ");
// Read user input
scanf("%d", &n);
// Call SieveOfEratosthenes function with user input
SieveOfEratosthenes(n);
return 0;
}
Output
Enter the maximum number to find primes: 100
Prime numbers up to 100:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
Time and Space Complexity
- Time Complexity: The time complexity for this algorithm is O(n log log n). This makes it very efficient when dealing with large datasets.
- Space Complexity: The space requirement is O(n). This means that an array of size n+1 will be needed to hold true or false for every number until n itself included.
Applications of the Sieve of Eratosthenes
It is used in various fields which include:
- When large prime factors are used in Cryptography.
- By algorithms that factorize large integers or are used to perform primality tests.
- For generating keys in public-key encryption systems under network security.
- Mathematical software and scientific computing where prime numbers play a significant role.
Conclusion
The Sieve of Eratosthenes is still considered as one of the most efficient and practical ways to find all prime numbers less than an upper bond. Understanding how these types of algorithms work enables one to see the beauty behind them and appreciate mathematical concepts as powerful tools for solving real-life computing problems.
Similar Reads
Output of C programs | Set 32 Predict the output of the following C programs.1. What will be the output of following program? Input: 1 3 C #include<stdio.h> int main() { int a, b; if(scanf("%d%d", &a, &b)==2) printf("true"); else printf("false"); return 0; } Output: True Explanation: S
3 min read
Output of C programs | Set 38 Ques1. What is the output of the following ? C #include <stdio.h> void main() { int a[5] = { 5, 1, 15, 20, 25 }; int i, j, m; i = ++a[1]; j = a[1]++; m = a[i++]; printf("%d, %d, %d", i, j, m); } Options : A. 3, 2, 15 B. 2, 3, 20 C. 2, 1, 15 D. 1, 2, 5 Answer : A Explanation : >
3 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
Top 50 C Coding Interview Questions and Answers (2025) C is the most popular programming language developed by Dennis Ritchie at the Bell Laboratories in 1972 to develop the UNIX operating systems. It is a general-purpose and procedural programming language. It is faster than the languages like Java and Python. C is the most used language in top compani
15+ min read
Java Program to Implement Sieve of Eratosthenes to Generate Prime Numbers Between Given Range A number which is divisible by 1 and itself or a number which has factors as 1 and the number itself is called a prime number. 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. Example: Input : from = 1, to = 20 Out
3 min read
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, 19Approach 1: In the below program, the range of numbers is taken as input and stored in the variab
5 min read