您的位置:首页 > 编程语言 > C语言/C++

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

2017-11-24 17:22 204 查看

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

Problem Description

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

Input

输入整数n(n<10000)。

Output

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

Example Input

100


Example 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


代码:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()
{
int i, n, j, count = 0;
scanf("%d", &n);
for(i = 2; i <= n; i++)
{
for(j = 2; j <= sqrt(i); j++)
{
if(i % j == 0)
{
break;
}
}
if(j > sqrt(i))
{
printf("%d ", i);
count++;
if(count % 10 == 0)
{
printf("\n");
}
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: