(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;
}

运行代码截图:

注:侵权可删

相关推荐
lsylalalala39 分钟前
常见的排序算法1
数据结构·算法·排序算法
想吃火锅10051 小时前
【leetcode】54. 螺旋矩阵
算法·leetcode·矩阵
wuminyu1 小时前
JVM利用io_uring优化堆外内存性能原理剖析
java·linux·c语言·jvm·c++
想吃火锅10052 小时前
【leetcode】300. 最长递增子序列
算法·leetcode·职场和发展
imaol12 小时前
数据结构---队列
java·数据结构·算法
数模竞赛Paid answer2 小时前
2026年电工杯数学建模A题绿电直连型电氢氨园区优化运行求解全过程论文及程序
算法·数学建模·电工杯
疯狂打码的少年2 小时前
【数据结构】串的模式匹配:KMP算法(重点)
数据结构·笔记·算法
晚风醉蝶3 小时前
第零篇-算法全景图
算法
疯狂打码的少年3 小时前
【数据结构】树的基本概念与二叉树定义
java·数据结构·笔记·算法
webmote333 小时前
用 NVIDIA Nemotron 3 Super + .NET 构建有记忆的多轮对话
后端·算法