• Source
    1. #include<stdio.h>
    2. #include<math.h>
    3. #define N 7000
    4. bool prime[7009]={false}; // let, all are prime
    5.  
    6. void pri()
    7. {
    8. int i, j, sqrtN;
    9. sqrtN = int( sqrt( N ) ); // We have to check primes up to (sqrt(N))
    10.  
    11. for( i = 3; i <= sqrtN; i += 2 ) {
    12. if( prime[i]== false ) { // so, i is a prime, so, discard all the multiples
    13. // j = i * i, because it’s the first number to color
    14. for( j = i*i; j <= N; j += 2*i )
    15. prime[j] = true;
    16. }
    17. } ///********* Thanks To Jan Vai ********
    18. }
    19.  
    20. int main()
    21. {
    22. pri();
    23. int i,j=0;
    24. printf("2 "); // print the primes
    25.  
    26. for( i = 3; i <= N; i += 2 ) { // increment is 2, to avoid the even number
    27. if( prime[i] == false ) {
    28. printf("%d ", i);
    29. ++j;
    30. }
    31. if(j%25==0)
    32. puts("");
    33. }
    34.  
    35. printf("\n\n******** The total prime number is %d *******\n",j);
    36. return 0;
    37. }