C/C++练习7---求某个范围内的所有素数

Problem Description

求小于n的所有素数,按照每行10个显示出来。

Input

输入整数n(n<10000)。

Output

每行10个依次输出n以内的所有素数。如果一行有10个素数,每个素数后面都有一个空格,包括每行最后一个素数。

Sample Input

100

Sample Output

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 

Hint

Source

代码:

#include<stdio.h> 
int main(){ 
    int i,j,a,count; 
    scanf("%d",&a); 
    int k=0; 
    for(i=2;i<=a;i++){ 
        count=0; 
        for(j=1;j<=i;j++){ 
            if(i%j==0){ 
                ++count; 
            } 
        } 
        if(count==2){ 
            printf("%d ",i); 
            ++k; 
            if(k%10==0) 
                printf("\n"); 
        } 
    }  
} 

猜你喜欢

转载自blog.csdn.net/ailmengi000/article/details/79842980