#include <stdio.h>#include <stdlib.h>/**
用递归求前n项和
*/long sum(int n){ if(1==n) return 1; else
return sum(n-1)+n;
}int main(){ int n; printf("请输入需要求前n项和的n:"); scanf("%d",&n); printf("你所要求的前n项和为:%ldn",sum(n)); return 0;
}
运行结果

image.png
作者:桓宇Harry
链接:https://www.jianshu.com/p/69a5911d9ef2
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/4363/viewspace-2820901/ ,如需转载,请注明出处,否则将追究法律责任。