帮忙做几道c++题目

来源:百度知道 编辑:UC知道 时间:2024/06/05 03:21:36
1.编写一个函数用递归的方法求1+2+3+4+……+n的值。在主函数中进行输入输出。
2.编写一个函数用递归的方法求n!的值。在主函数中进行输入输出。
晕我在网上找都找不到所以来求救。希望大大门能帮忙,这个学期的补考类型。对这些我是白白,求求你们帮忙!
谢谢1楼的。以为我不是这个专业的,学的也是C语言,在说也忘光光了,挂科就不能拿毕业证了,所以得硬着头皮过了.非常感谢2楼在HI知道的帮助。谢谢4楼的可以了。也很感谢blaze对不起只能有一个答案谢谢你!

1、#include <iostream.h>
long sum(int n)
{
long f;
if (n<=0) cout<<"n<0,data error!"<<endl;
else if (n==1) f=1;
else f=sum(n-1)+n;
return(f);
}

void main()
{
int n;
long y;
cout<<"Enter a positive integer:";
cin>>n;
y=sum(n);
cout<<n<<"="<<y<<endl;
}
2、
#include <iostream.h>

long fac(int n)
{
long f;
if (n<0) cout<<"n<0,data error!"<<endl;
else if (n==0) f=1;
else f=fac(n-1)*n;
return(f);
}

void main()
{
int n;
long y;
cout<<"Enter a positive integer:";
cin>>n;
y=fac(n);
cout<<n<<"!="<<y<<endl;
}
希望你能通过考试.

//1 递归求和函数
long SumN(long n = 1)
{
if (1 == n)
{