1到100的的素数
大概这样子的 int a,b,cfor(a=0;a<100;a++)
后面我不会了 #include <stdio.h>
int prime(int n)
{
int i , r = 0 ;
if(n > 1) {
if(n % 2) {
for(r = 1 , i = 3 ; i * i < n + 1 ; i += 2) {
if(! (n % i)) {
r = 0 ;
break ;
}
}
} else if (n == 2) r = 1 ;
}
return r ;
}
int main(void)
{
int c , i ;
for(c = 0 , i = 1 ; i < 101 ; i ++) {
if(prime(i)) {
if(c) printf("\t") ;
printf("%2d" , i) ;
c ++ ;
}
}
printf("\n") ;
}
编译运行实况
D:\00.Excise\C>g++ -o x1 x1.c
D:\00.Excise\C>x1
2 3 5 7 11 13 17 19 23 29
31 37 41 43 47 53 59 61 67 71
73 79 83 89 97
D:\00.Excise\C> #include <stdio.h>
void main()
{
int a, b, n = 0, c;
for (a = 0;a <= 100;a++)
{
for (b = 2;b < 100;b++)
{
if (a % b == 0)
break;
}
if (a == b)
{
c = a;
n++;
}
}
for(a = 0;a <= n;a++);
printf("%d ", c);
printf("\n");
}
页:
[1]