Print the Prime Numbers from a List of Numbers, say, 1 to 1000. - C Data Type

C examples for Data Type:int

Description

Print the Prime Numbers from a List of Numbers, say, 1 to 1000.

Demo Code

#include <stdio.h>
#include <math.h>
#define SIZE 1000//  ww w  .  ja va2 s .  c om

int status[SIZE];

void sieve()
{
     int i, j, sq;
     for(i = 0; i < SIZE; i++) {
         status[i] = 0;
     }

     sq = sqrt(SIZE);

     for(i=4;i<=SIZE;i+=2) {
        status[i] = 1;
     }

     for(i = 3; i <= sq; i += 2)
     {
         if(status[i] == 0){
              for(j = 2*i; j <= SIZE; j += i)
                 status[j] = 1;
         }
     }
     status[1] = 1;
}

int main(){
     int i, counter = 1000;
     sieve();
     printf("\nFollowing numbers are prime in the range: 1 to %d :\n", counter);
     for (i = 1; i < counter; i++){
       if(status[i]==0) {
          printf("%d\t", i);
       }
     }

     return 0;
}

Result


Related Tutorials