我也来试试……
#include<stdio.h>
void main()
{ int sum(int i); int n; int result; printf("please input the number\n"); scanf("%d",&n); result=sum(n); printf("the result is %d\n",result);
}
int sum(int i)
{ int result; if(i==1) result=1; else if(i>1) result=i+sum(i-1); return(result);
}
TC2.0上编译通过
int sum(int n);
int main()
{ int n; printf("Please enter the number n: (q to quit)"); while(scanf("%d",&n)==1) { printf("%d\n", sum(n)); printf("Please enter the number n: (q to quit)");
} return 0;
}
int sum(int n)
{ if (n ==1) { return 1; } else return sum(n - 1) + n;
}