输出小于等于n的素数
程序员文章站
2024-03-14 20:54:29
...
#include<iostream>
#include<math.h>
using namespace std;
int n = 0;
void prime_number(int num)
{
int k = 0, i = 2;
if (num >= 0)
{
k = (int)sqrt((double)num);
while (i <= k)
{
if (num % i == 0)
break;
i++;
}
if (i > k)
{
cout << num << " ";
n++;
if (n > 9)
{
cout << endl;
n = 0;
}
}
}
}
int main()
{
int num = 0;
cout << "输入一个大于2的正整数:" << endl;
cin >> num;
cout << "素数有:" << endl;
for (int i = 2; i <= num; i++)
prime_number(i);
}