(C语言)求出1!+2!+3!+...+10!的值

方法1:迭代

cpp 复制代码
#include<stdio.h>
int main()
{
	
	long sum = 0,t = 1;
	for(int i = 1;i <= 10;i ++)
	{
		t *= i;
		sum += t;
	 } 
	 printf("%ld\n",sum);
	return 0;
}

方法2:递归

cpp 复制代码
#include<stdio.h>
#define N 10
long factorial(int n)
{
	if(n <= 1)
		return 1;
	return n*factorial(n-1);
}
int main()
{
	long sum = 0;
	for(int i = 0;i <= N;i ++)
		sum += factorial(i);
	printf("%ld\n",sum);
	return 0;
}

运行代码截图:

注:侵权可删

相关推荐
写代码写到手抽筋1 天前
线性插值与Sinc插值的数学原理及实战
算法
孤飞1 天前
zero2Agent:面向大厂面试的 Agent 工程教程,从概念到生产的完整学习路线
算法
技术专家1 天前
Stable Diffusion系列的详细讨论 / Detailed Discussion of the Stable Diffusion Series
人工智能·python·算法·推荐算法·1024程序员节
csdn_aspnet1 天前
C# (QuickSort using Random Pivoting)使用随机枢轴的快速排序
数据结构·算法·c#·排序算法
鹿角片ljp1 天前
最长回文子串(LeetCode 5)详解
算法·leetcode·职场和发展
噜噜大王_1 天前
深入理解 C 语言内存操作函数:memcpy、memmove、memset、memcmp
c语言·开发语言
paeamecium1 天前
【PAT甲级真题】- Cars on Campus (30)
数据结构·c++·算法·pat考试·pat
chh5631 天前
C++--模版初阶
c语言·开发语言·c++·学习·算法
RTC老炮1 天前
带宽估计算法(gcc++)架构设计及优化
网络·算法·webrtc
dsyyyyy11011 天前
计数孤岛(DFS和BFS解决)
算法·深度优先·宽度优先