很急,大家帮我写一下用C++1的阶成加5的阶成之和

来源:百度知道 编辑:UC知道 时间:2024/07/07 21:19:45

#include<iostream>
using namespace std;
int fac(int n)
{//递归求阶乘
if(n<0)cout<<"数据错误!";
if(n==0||n==1)return 1;
else return n*fac(n-1);
}
void main()
{ int n;
cout<<"please input a number:"<<endl;
cin>>n;
cout<<fac(n);
//求的是n的阶层 输入5即为楼主想要
}

int fac(int n)
{//递归求阶乘
if(n<0)cout<<"数据错误!";
if(n==0||n==1)return 1;
else return n*fac(n-1);
}
在主函数里面调用这个函数就行了

#include<iostream>
long fun(int n)
{
long M=1;
for(i=2;i<=n;i++)
M=M*i;
return M;
}
void main(void)
{
std::out<<"1!+5!="<<fun(1)+fun(5);
}