#include<stdio.h>
#include<iostream>
#include<iomanip>//setw函数头文件
using namespace std;
int main()
{
int n, i, t, b = 0;
cout << "素数判断程序" << endl;
cout << "请输入一个自然数n" << endl;
cout << "n=";
cin >> n;
if (n < 0)
{
for (i = 1; i < 999999999999999999; i++)
{
cout << " 请输入一个自然数,谢谢" << endl;
n = 0;
cout << "n=";
cin >> n;
if (n >= 0) //设置循环系统,目的就是让你输入自然数
{
break;
}
}
}
if(n>=0)
{
if (n == 0)
{
cout << "";
}
else
{
cout << "n的因式有:" << endl;
for (i = 1; i <= n; i++)
{
t = n % i;
if (t == 0)
{
b++; //统计因式数量
cout << left<<setw(9) << i; //输出因式
}
}
cout << "" << endl;
}
}
if (b == 2 && n != 0) //素数判断
{
cout << "n为素数" << endl;
}
else
{
cout << "n不为素数,他有" << b << "个因式" << endl;
}
return 0;
}
运行结果