当前位置:网站首页>[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 !!!️
边栏推荐
猜你喜欢

oSPF基础实验配置

【C语言程序设计】分支结构

First acquaintance with C language (1)

HCIP第一天静态路由综合实验
![[C language] relevant distinction between strlen and sizeof](/img/c0/c026818692a01c1867771434e90da8.png)
[C language] relevant distinction between strlen and sizeof

C language -- nesting of relational and logical operators, if statements, switch statements, and branch structures

Hcip OSPF comprehensive experiment

Codeforces Round #809 (Div. 2), problem: (C) Qpwoeirut And The City

(CF1691D) Max GEQ Sum

(CF1691D) Max GEQ Sum
随机推荐
(前缀和/思维)Codeforces Round #806 (Div. 4)F. Yet Another Problem About Pairs Satisfying an Inequality
Lecture 3 - GPIO input / output library function usage and related routines
MySQL课程2.表的各种查询
OSPF basic configuration application (comprehensive experiment: interference election default routing area summary authentication -- interface authentication)
(CF1691D) Max GEQ Sum
(CF1691D) Max GEQ Sum
HCIP-第一天
Educational Codeforces Round 132 (Rated for Div. 2), problem: (D) Rorororobot
Wechat applet: user wechat login process (attached: flow chart + source code)
OSPF路由信息协议-拓扑实验
C语言——字符和字符串、算术运算符、类型转换
HCIP-第二天
Detailed source code of golang bufio reader
SQL优化的N种方法
oSPF基础实验配置
Codeforces Round #809 (Div. 2), problem: (C) Qpwoeirut And The City
C language - characters and strings, arithmetic operators, type conversions
The basic configuration of static routing (planning of IP address and configuration of static routing) realizes the accessibility of the whole network.
HCIP oSPF综合实验
Hcip first day static routing comprehensive experiment
