非递归
#include<stdio.h>
int main()
{
int n;
int x;
int sum=0;
printf(“输入一个整数 n = “);
scanf(”%d”,&n);
for(x = 1; x<= n; x++ )
{
sum += x;
}
printf (“序列求和 sum = %d\n”,sum);
return 0;
}
递归
#include<stdio.h>
int Sum(int n)
{
if(n == 0 || n ==1 )
return 0;
else
return Sum(n-1)+n;
}
int main()
{
printf("%d\n",Sum(10000));
}