题目:编写程序,输出结果小于50000的正整数的阶乘值。想一想若用while(1){}构造循环,循环条件是什么?有什么方法可以结束循环?
for循环:
#include<iostream>
using namespace std;
int main()
{
cout << "Output the factorial of n(factorial is smaller than 50000):" << endl;
int fac = 1;
for (int i = 1;fac<=50000; i++)
{
fac *= i;
cout << "!" << i << "=" << fac << endl;
}
system("pause");
}
while循环:
#include<iostream> using namespace std; int main() { cout << "Output the factorial of n(factorial is smaller than 50000):" << endl; int fac = 1, i = 1; while (1) { fac *= i; if (fac <= 50000) { cout << "!" << i << "=" << fac << endl; } else break; i++; } system("pause"); }