void main()
{ int i, j, k=0; for (i = 2; i <= 200;i++) { j = 2; while (i%j != 0) j++; if (i == j) { printf("%8d", i); k++; if (k % 7 == 0) printf("\n"); } }
x 赋值为2 应该放在外层循环内部。 y 变量虽然求得了除1和n外的因子数,但仅仅用于确认素数,纯属多余。
#include <stdio.h>
int main ( void )
{ int n = 2, x, y = 0, z; while ( n <= 200 ) { x = 2; while ( x < n ) { z = n % x; if ( z == 0 ) y = y + 1; x++; } if ( y == 0 ) printf( "%d ", n ); n++; y = 0; } return 0;
}
#include <stdio.h>
int main ( void )
{ int n, x; for ( n = 2; n <= 200; n++ ) { for ( x = 2; x < n && n % x; x++ ) ; if ( x >= n ) printf( "%d ", n ); } return 0;
}