This C program implements the famous Sieve of Eratosthenes algorithm to generate prime numbers up to a certain limit. The Sieve of Eratosthenes is an efficient method to find all prime numbers within a specified range.
The program will prompt the user to input a positive integer, which will serve as the upper limit for prime number generation. It will then apply the Sieve of Eratosthenes algorithm to find all prime numbers up to and including this limit.
The algorithm works as follows:
- Create a boolean array of size (limit + 1) to store the primality of numbers from 0 to the limit.
- Initialize all elements of the array to
true
. - Starting from 2 (the first prime number), iterate through the array. For each prime number found, mark its multiples as
false
in the array, as they are not prime numbers. - After the iteration, all remaining elements marked as
true
in the array are prime numbers.
To run this program, you need a C compiler installed on your system. If you don't have one, you can install GCC (GNU Compiler Collection), which is freely available and widely used.
To compile the C program, use the following command in the terminal:
gcc -o prime_generator prime_generator.c -lm
After compiling, run the executable with the following command:
./prime_generator
-
Upon running the program, you will be prompted to enter a positive integer that will act as the upper limit for prime number generation.
-
Enter the desired limit and press Enter.
-
The program will then generate and display all the prime numbers up to the specified limit.
-
The output will be a list of prime numbers printed on the console.
Let's say you run the program and input the limit as 20.
Input:
Enter the limit for prime number generation: 20
Output:
Prime numbers up to 20 are:
2 3 5 7 11 13 17 19
If you find any bugs, issues, or have suggestions for improvement, please feel free to contribute! You can fork the repository, make your changes, and create a pull request.