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

如题所述

//用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
温馨提示:答案为网友推荐,仅供参考
第1个回答  2020-02-26
//用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
相似回答