问题描述
用C++实现输入一个 10 至 100 范围内的整数,输出它的所有因子。如果输入数字超出范围,输出“Error!”
实现程序
#include<iostream>
using namespace std;
int main()
{
int num;
cout<<"输入一个10~100的正整数:";
cin>>num;
cout<<num<<"的因子为:"<<endl;
if(10<=num&&num<=100) //限制10~100的正整数
{
for(int i=1;i<=num;i++) //求num的因子
{
if(num%i==0)
cout<<i<<'\t'; //转义字符\t:跳转到下一个tab位置
}
}
else
cout<<"Error!"<<endl;
return 0;
}
实验结果(以输入99和101为例):
输入一个10~100的正整数:99
99的因子为:
1 3 9 11 33 99
--------------------------------
Process exited after 33.58 seconds with return value 0
请按任意键继续. . .输入一个10~100的正整数:101
101的因子为:
Error!--------------------------------
Process exited after 8.277 seconds with return value 0
请按任意键继续. . .