示例程序:
#include<stdio.h>
#include<math.h>
int main()
{
int count = 0, num = 0;
int MAXN;
printf("please enter MAXN:\n");
scanf_s("%d", &MAXN);
printf("%d以内素数是:\n",MAXN);
for (int i = 2; i <= MAXN; i++)
{
for (int j = 2; j <= sqrt(i); j++)
{
if (i%j == 0)
count++;
}
if (count == 0)
{
printf("%-5d ", i);
num++;
if (num % 10 == 0)
printf("\n");
}
else
{
count = 0;
continue;
}
}
printf("\n以内素数共计 %d 个 \n", num);
return 0;
}
网友评论