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

运行代码截图:

注:侵权可删

相关推荐
春风解人意3 小时前
从零开始学习嵌入式P32----网络基础之TCP
c语言·网络·学习·tcp/ip
政企项目老覃6 小时前
大模型幻觉治理与自动评测:金融风控场景的落地实践
人工智能·算法·机器学习
是隼人6 小时前
buuctf-pwn inndy_echo(32位fmt)题解(学习过程持续更新)
c语言·学习·安全·pwn入门·ctf入门
淡海水6 小时前
08-03-不可变-ImmutableDictionary-TKey-TValue-与ImmutableHashSet-T-持久化哈希树
数据结构·算法·c#·哈希算法·dictionary·immutable
hansang_IR6 小时前
【题解】[APIO2023] 赛博乐园 / cyberland
c++·算法·图论
洛阳纸贵6 小时前
MATLAB-matlab基础知识
学习·算法·matlab
落羽的落羽7 小时前
【AI】快速理解AI应用的相关名词概念
linux·c++·人工智能·python·计算机网络·算法
Nil2087 小时前
leetcode 17电话号码的字母组合
算法·leetcode·职场和发展
刃神太酷啦7 小时前
Redis 进阶核心:持久化 (RDB/AOF)、事务与主从复制全解析----《Hello Redis!》(5)
linux·c语言·数据库·c++·redis·缓存·bootstrap