分别计算5!到9!的值,使用setw()控制“=”左边的数值宽度。
#include<iostream>
#include <iomanip>
using namespace std;
double fact(int n);
int main()
{ for(int n=5;n<10;n++)
cout<<setw(2)<<n<<"!= "<<fact(n)<<endl;
return 0;
}
double fact(int n)
{ double factor=1;
for(int i=n; i>=1; i--)
factor*=i;
return factor;
}
本文展示了如何使用C++计算从5到9的阶乘,并使用setw()函数控制输出中数字的宽度。代码简洁高效,适用于理解C++中的基本数学运算和输出格式化。

被折叠的 条评论
为什么被折叠?



