计算数的阶乘,分别用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

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网