C语言应用递归调用的方法分别求1+2+3.......+100或N!

2024-11-09 09:41:27
推荐回答(4个)
回答1:

///


/// 计算小于某整数的加法、乘法
///

/// 为真是加,假为乘
/// 最大数
/// 返回值
public double compute(bool Ctype, int endNum)
{
double tempNum = 0;

if (endNum-1 > 0)
compute(Ctype, endNum-1);

if (Ctype)
{
return tempNum + endNum;
}
else
{
if (tempNum = 0)
tempNum = 1;

return tempNum * endNum;
}
}

回答2:

int sum(int n)
{
if(n==1)
return 1;//出口

return sum(n-1)+n;
}
main()
{
int n=100;
printf("%d",sum(n));
}

回答3:

#include "stdio.h"
int sum(int num)
{
if(num == 1)
return 1;
else
return sum(num-1)+num;
}
main()
{
int num = 100;
printf("答案为:%d\n",sum(100));
}

回答4:

书本问题....