C++使用while for do...while分别计算一个数字的阶乘
要求编写三个程序,用三种循环分别实现
C++使用while for do...while分别计算一个数字的阶乘
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
1条回答 默认 最新
mewiteor 2014-12-23 05:57关注while:
#include<iostream> using namespace std; int main() { unsigned long long result=1,n; cin>>n; while(n)result*=n--; cout<<result<<endl; return 0; }for:
#include<iostream> using namespace std; int main() { unsigned long long result,n; for(result=1,cin>>n;n;--n)result*=n; cout<<result<<endl; return 0; }do...while:
#include<iostream> using namespace std; int main() { unsigned long long result=1,n; cin>>n; if(n) do { result*=n; }while(--n); cout<<result<<endl; return 0; }评论 打赏 举报解决 2无用 1