C语言的!帮我看一下哪里错了!

来源:百度知道 编辑:UC知道 时间:2024/05/16 10:29:25
#include "stdio.h"
int SUM(int n)
{
int temp=0;
if(n==1)
temp=1;
temp=SUM(n-1)+n+temp;
return temp;
}
void main()
{
int sum;
int n=5;
clrscr();
sum=SUM(n);
printf("%d\n",sum);
}

朋友在递规时好像没有使用else,应该是加上就可以了

#include "stdio.h"
int SUM(int n)
{
int temp=0;
if(n==1)
temp=1;
else
{
temp=SUM(n-1)+n+temp;
}
return temp;
}
void main()
{
int sum;
int n=5;
clrscr();
sum=SUM(n);
printf("%d\n",sum);
}

你的这个程序没有错啊---
Turbo C For Windows 3.1 正在为您编译....

g:\c语言\win32\wenti.c:
正在为您连接...

可用内存 431876

★★★编译成功! 花费时间: 0.281 秒.

这是我用编译器验证了的,只是你什么也没有让它做,子函数中,如果n等于1,就怎么怎么样,否则就什么也不做,你主函数的实际参数传去了一个5,没有等于1,所以什么也没有做.

同意一楼的