#include "stdafx.h"#include <stdlib.h>,我来为大家科普一下关于c语言求1000以内的素数?以下内容希望对你有帮助!
c语言求1000以内的素数
C语言学习 程序练习 求一个正整数以内的素数代码如下:#include "stdafx.h"
#include <stdlib.h>
int main()
{
int i,j=2;
int value;
int prime;
bool flag=false;
scanf_s("%d", &value);
for (i = 2; i < value; i )
{
for (j = 2; j <i; j )
{
prime = i%j;
if (prime == 0)
{ flag = true;
break; }
else
{ flag = false; }
}
if (flag==false && i>=2)
{ printf("this is prime %d in below %d \n",i,value);}
}
system("pause");
return 0;
}
运行结果:
输入100
this is prime 2 in below 100
this is prime 3 in below 100
this is prime 5 in below 100
this is prime 7 in below 100
this is prime 11 in below 100
this is prime 13 in below 100
this is prime 17 in below 100
this is prime 19 in below 100
this is prime 23 in below 100
this is prime 29 in below 100
this is prime 31 in below 100
this is prime 37 in below 100
this is prime 41 in below 100
this is prime 43 in below 100
this is prime 47 in below 100
this is prime 53 in below 100
this is prime 59 in below 100
this is prime 61 in below 100
this is prime 67 in below 100
this is prime 71 in below 100
this is prime 73 in below 100
this is prime 79 in below 100
this is prime 83 in below 100
this is prime 89 in below 100
this is prime 97 in below 100
,