当前位置:网站首页>【C语言】阶乘实现
【C语言】阶乘实现
2022-07-26 22:49:00 【helinliupi-何】
博客主页:Luo-Kuang-何
座右铭:一起走向人生巅峰的路上
学习进度:【C语言】
博客声明:我将尽我所能,用心写好每一份博客,让更多小伙伴能够与我交流学习心得。
如有不足,请指正。学习就是在不断犯错并改正的过程!
文章目录
前言:
在现实中,我们做数学题总会遇到阶乘问题,这在计算机中也不例外。 那我们应该怎么实现呢?
我记得很多老师在电脑上书写阶乘都是用!这个符号表示。 比如5的阶乘,写为5!。 这在C语言中是行不通的,下面我讲解C语言中阶乘的实现。
1.阶乘实现
1.1理论步骤
我们可以利用while、do……while、以及for等循环实现,实现功能如下:
- 我们先设置好3个变量,i、n(想要的阶层数)、jieceng(阶层结果)
- 利用循环得到相应阶乘
1.2实践结果
这里用3种循环都展示一下
while实现
#include <stdio.h>
int main()
{
int i = 1;
int n = 0;
int jieceng = 1;
scanf("%d", &n); //输入想得到的阶层数
while (i <= n)
{
jieceng *= i;
i++;
}
printf("%d的阶层是%d\n", n, jieceng);
return 0;
}

do……while实现
#include <stdio.h>
int main()
{
int i = 1;
int n = 0;
int jieceng = 1;
scanf("%d", &n);
do
{
jieceng *= i;
i++;
} while (i <= n);
printf("%d的阶层是%d\n", n, jieceng);
return 0;
}for实现
#include <stdio.h>
int main()
{
int i = 1;
int n = 0;
int jieceng = 1;
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
jieceng *= i;
}
printf("%d的阶层是%d\n", n, jieceng);
return 0;
}2.连续乘层相加实现
2.1理论步骤
- 首先明确要连续相加,必然需要2层循环嵌套
2.2实践结果
我这里用2层for循环实现
#include<stdio.h>
int main() //算1~n阶层相加
{
int sum = 0;
int jiecen = 0;
int i = 0;
int j = 0;
int n = 0;
scanf("%d", &n); //设置想要到达的n值
for (j = 1; j <= n; j++)
{
for (i = 1, jiecen =1; i <= j; i++) //重置jieceng,使每次从1开始
{
jiecen = jiecen * i; //里循环得每一相应阶层数
}
sum += jiecen; //阶层之和
}
printf("%d\n", sum);
return 0;
} 
一层for循环实现(简洁版)
#include <stdio.h>
int main() //算1~10阶层相加(j简短版)好**
{
int sum = 0;
int jiecen = 1;
int i = 0;
int n = 0;
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
jiecen = jiecen * i;
sum += jiecen;
}
printf("%d\n", sum);
return 0;
}总结:
实现阶乘有很多方法,还需要我们去探索,以上是我的一些理解。
结语:
各位小伙伴们,觉得有用就给波三连呗!如有错误,请指正,谢谢大家!如果大家有不同意见,请与我交流,一起进步
我们顶峰相见!!!️
边栏推荐
- Nb-iot access to cloud platform
- RS-485 bus communication application
- Codeforces Round #796 (Div. 2), problem: (1688C) Manipulating History
- Esp8266wi fi access cloud platform
- Codeforces Round #807 (Div. 2), problem: (C) Mark and His Unfinished Essay
- OSPF静态大实验
- 6.30 didi surface warp (one side + two sides)
- Golang中的错误处理
- 2022 latest Tiktok live broadcast monitoring (II) streaming media download in live broadcast room
- (题意+详细思路+加注释代码) Codeforces Round #805 (Div. 3)F. Equate Multisets
猜你喜欢

Codeforces Round #807 (Div. 2), problem: (C) Mark and His Unfinished Essay

OSPF协议知识汇总

静态路由缺省路由vlan实验

Experiment of total connection and star topology of mGRE

多线程中 synchronized 锁升级的原理是什么?

HCIA Basics (1)

(title + detailed idea + annotated code) codeforces round 805 (Div. 3) F Equate Multisets

STM32入门教程第二讲

Text to image paper intensive reading ssa-gan: text to image generation with semantic spatial aware Gan

WAN technology experiment
随机推荐
OSPF configuration in mGRE environment and LSA optimization - reduce the amount of LSA updates (summary, special areas)
Lora gateway node converges sensor data
JUC并发编程
6.28 Dahua written examination
2022 latest Tiktok live broadcast monitoring (II) streaming media download in live broadcast room
Ospf基础配置应用( 综合实验: 干涉选举 缺省路由 区域汇总 认证--接口认证)
2022 latest live broadcast monitoring 24-hour monitoring (III) analysis of barrage in live broadcast room
识时务者常用网址大全
7.7 sheen Xiyin written test
2022zui new Tiktok 24-hour round robin live broadcast monitoring (I) live broadcast room start-up monitoring
C语言——数组、字符串处理函数、strlen、strcpy和strncpy、strcat和strncat、strcmp和strncmp
NB-IOT联网通信
Lora光照传感器节点数据采集
Lora communication application development
Experiment of total connection and star topology of mGRE
7.13 Weilai approved the written examination in advance
Lora illumination sensor node data acquisition
记录HandsomeBlog的star用户
Republishing and routing strategy of OSPF
TIM输出比较——PWM
