#include <iostream>
using namespace std;
long factorial(int a);
int main() {
int m;
cout<<"Input a number:(q to quit)";
while(cin>>m)
{
cout<<m<<"!= "<<factorial(m)<<endl;
cout<<"next input:";
}
return 0;
}
long factorial(int a)
{
long result;
if(a>0)
result=a*factorial(a-1);
else
result=1;
return result;
}
C++primer plus 6th 第7章7.5编程答案
最新推荐文章于 2025-11-30 21:05:18 发布
本文介绍了一种使用递归算法实现阶乘计算的方法。通过C++编程语言,我们定义了一个名为factorial的函数,该函数接收一个整数作为参数,并返回其阶乘结果。在主函数中,程序会持续读取用户输入的数值并调用factorial函数计算阶乘,直到用户选择退出。
956

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



