㈠ 計算數的階乘,分別用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