当前位置:网站首页>[C language] factorial implementation
[C language] factorial implementation
2022-07-27 02:24:00 【Helinliupi he】
Blog home page :Luo-Kuang- What
motto : On the way to the peak of life together
Learning progress :【C Language 】
Blog statement : I will try my best to , Write every blog carefully , Let more friends exchange learning experiences with me .
If there is any deficiency , Please correct me. . Learning is the process of constantly making mistakes and correcting !
List of articles
2. Continuous factorial addition implementation
Preface :
In reality , We always encounter factorial problems when doing mathematical problems , This is no exception in computers . So how do we do it ?
I remember that many teachers write factorials on computers using ! This symbol means . such as 5 The factorial , Written as 5!. This is in C It doesn't work in language , Let me explain C Implementation of factorial in language .
1. Factorial realization
1.1 Theoretical steps
We can use while、do……while、 as well as for Equal cycle implementation , The functions are as follows :
- Let's set up 3 A variable ,i、n( The number of classes you want )、jieceng( Hierarchical results )
- Use the cycle to get the corresponding factorial
1.2 The practice results
Here we use 3 Show me all the cycles
while Realization
#include <stdio.h>
int main()
{
int i = 1;
int n = 0;
int jieceng = 1;
scanf("%d", &n); // Enter the desired number of levels
while (i <= n)
{
jieceng *= i;
i++;
}
printf("%d The class is %d\n", n, jieceng);
return 0;
}

do……while Realization
#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 The class is %d\n", n, jieceng);
return 0;
}for Realization
#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 The class is %d\n", n, jieceng);
return 0;
}2. Continuous multiply layer addition
2.1 Theoretical steps
- First of all, it is clear to add continuously , Necessity 2 Layer loop nesting
2.2 The practice results
I use it here. 2 layer for Cycle to achieve
#include<stdio.h>
int main() // count 1~n Add layers
{
int sum = 0;
int jiecen = 0;
int i = 0;
int j = 0;
int n = 0;
scanf("%d", &n); // Set the desired n value
for (j = 1; j <= n; j++)
{
for (i = 1, jiecen =1; i <= j; i++) // Reset jieceng, Make every time from 1 Start
{
jiecen = jiecen * i; // The number of each corresponding class in the cycle
}
sum += jiecen; // The sum of classes
}
printf("%d\n", sum);
return 0;
} 
First floor for Cycle to achieve ( Concise Edition )
#include <stdio.h>
int main() // count 1~10 Add layers (j Short version ) good **
{
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;
}summary :
There are many ways to implement factorials , We still need to explore , These are some of my understandings .
Conclusion :
Dear friends , If you think it's useful, just give it to Bo Sanlian ! If there is a mistake , Please correct me. , Thank you. ! If you have different opinions , Please communicate with me , Progress together
We met at the summit !!!️
边栏推荐
- Codeforces Round #810 (Div. 2), problem: (B) Party
- (title + detailed idea + annotated code) codeforces round 805 (Div. 3) F Equate Multisets
- NAT network address conversion experiment
- Timer interrupt experiment
- Educational Codeforces Round 132 (Rated for Div. 2), problem: (D) Rorororobot
- Lora网关节点汇聚传感器数据
- RIP路由信息协议-拓扑实验
- The basic configuration of static routing (planning of IP address and configuration of static routing) realizes the accessibility of the whole network.
- Republishing and routing strategy of OSPF
- Hcip OSPF knowledge summary
猜你喜欢

(prefix and / thinking) codeforces round 806 (Div. 4) F Yet Another Problem About Pairs Satisfying an Inequality

C语言——while语句、dowhile语句、for循环和循环结构、break语句和continue语句

Esp8266wi fi data communication

NAT网络地址转化实验

微信小程序:用户微信登录流程(附:流程图+源码)

WAN technology experiment

三个整数从大到小排序(详细介绍多种方法)

Hcip OSPF comprehensive experiment

C语言——数据类型、基本数据类型的取值范围

The latest C language introduction and advanced - the most complete and detailed C language tutorial in history!! Section 1 - overview of C language
随机推荐
First knowledge of C language (2)
睡不着时闭眼躺着,到底有没有用?
HCIP第一天静态路由综合实验
HCIA Basics (1)
Esp8266wi fi access cloud platform
C language -- while statement, dowhile statement, for loop and loop structure, break statement and continue statement
Timer interrupt experiment
入住博客第一天【冲八万】!
NB-IOT联网通信
HCIP 双向重发布以及路由策略
On the first day of staying in the blog [for 80000]!
OSPF basic configuration application (comprehensive experiment: interference election default routing area summary authentication -- interface authentication)
勤写标兵——云小井
TreeSet集合存储元素的问题
(史上最详细)Codeforces Round #805 (Div. 3)E. Split Into Two Sets
全网显示 IP 归属地,是怎么实现的?
OSPF configuration in mGRE environment and LSA optimization - reduce the amount of LSA updates (summary, special areas)
Lora illumination sensor node data acquisition
Hcip OSPF comprehensive experiment
golang 实现 tcp-聊天室
