计算数的阶乘,分别用while语句、do-while语句和for语句实现

来源:百度知道 编辑:UC知道 时间:2024/05/05 10:00:56

//用for
int n;
int result=1;
//在这里输入要计算阶乘的数n
for (int i = 1; i <= n; i++)
{
result *= i;
}
//在这里输出结果result

//用while
int n;
int result = 1;
//在这里输入要计算阶乘的数n
while (n>0)
{
result *= n--;
}
//在这里输出结果result

//用do while
int n;
int result = 1;
//在这里输入要计算阶乘的数n
do
{
result *= n--;
} while (n > 0);
if (result < 1) {
result = 1;
}
//在这里输出结果result