Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- int main (void)
- {
- printf("SIEVE OF ERATOSTHENES ALGORITHM\n");
- int num;
- printf("Give a number: ");
- scanf("%i", &num);
- printf("\nTo Display All Prime Numbers Between 1 and %i\n", num);
- int P[num];
- // initialize array to all zeros
- for (int i = 2; i < num; i++){
- P[i] = 0;
- }
- int i = 2;
- while(i < num){
- // 0 value indicates it is not marked as prime
- if (P[i] == 0){
- printf("%i ", i);
- }
- // set multiples of i to 1
- for (int j = 1; i * j <= num; j++){
- P[i*j] = 1;
- }
- // increment for next number to test
- i++;
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment