C language method to calculate the factorial of n: 1. Calculate the factorial through a for loop, code such as "for (i = 1; i 1)res...".
The operating environment of this tutorial: Windows 7 system, c99 version, Dell G3 computer.
How to calculate the factorial of n in C language?
Find the factorial of n in C language:
About To find the factorial problem of n, let’s look at a question first and use it to find the breakthrough point.
1. Problem
Problem Description
Given an integer n, find its factorial, 0≤n≤ 12
Input
Input a number n
Output
Output a number representing n Factorial
Sample Input
5
Sample Output
120
2. Analysis
Since we are looking for factorial, the breakthrough point is obvious.
The breakthrough point is : factorial
The concept and background of factorial:
1️⃣Concept:
The factorial of a positive integer (factorial ) is the product of all positive integers less than and equal to this number, and the factorial of 0 is 1. The factorial of a natural number n is written n!.
2️⃣Background:
In 1808, Christian Kramp (1760~1826) introduced this notation.
3️⃣ Calculation method of factorial:
Any natural number n greater than or equal to 1 Factorial representation method: n!=1×2 ×3×…×(n-1)×n or n!=n×(n-1)!Note: The factorial of 0 is 1, which is 0! =1. 1! = 1
2! = 2 * 1 = 2
3! = 3 * 2 * 1 = 6
…
n! = n * (n-1) *… * 2 * 1
3. Solve
Regarding the factorial of n implemented in C language, at the current introductory stage, we mainly have the following two ways of writing:First type: loop
①for loop
#include<stdio.h>int main(){ int n; scanf("%d", &n); int fact = 1; int i; for (i = 1; i Test sample: 5<p></p>1 * 2 * 3 * 4 * 5 = 120<p></p> <pre class="brush:php;toolbar:false">5120--------------------------------Process exited after 1.475 seconds with return value 0请按任意键继续. . .
②while loop
#include<stdio.h>int main(){ int n; scanf("%d", &n); int fact = 1; int i = 1; while (i Test example: 6<p></p>1 * 2 * 3 * 4 * 5 * 6 = 720<p> </p> <pre class="brush:php;toolbar:false">6720--------------------------------Process exited after 1.549 seconds with return value 0请按任意键继续. . .
Second type: recursion (function calls itself)
1️⃣Writing method one
#include <stdio.h>int Fact(int n);int main() //主函数{ int n, cnt; scanf("%d", &n); cnt = Fact(n); printf("%d\n", cnt); return 0;} int Fact(int n) //递归函数 { int res = n; if (n > 1) res = res * Fact(n - 1); return res;}</stdio.h>Test sample: 7
7 * 6 * 5 * 4 * 3 * 2 * 1
= 1 * 2 * 3 * 4 * 5 * 6 * 7
= 5040
75040--------------------------------Process exited after 2.563 seconds with return value 0请按任意键继续. . .Of course it can also be written like this :
2️⃣Writing method two
#include <stdio.h>int Fact(int n) //递归函数 { int res = n; if (n > 1) res = res * Fact(n - 1); return res;}int main() //主函数 { int n, cnt; scanf("%d", &n); cnt = Fact(n); printf("%d\n", cnt); return 0;}</stdio.h>Test sample: 66 * 5 * 4 * 3 * 2 * 1
= 1 * 2 * 3 * 4 * 5 * 6
= 720
6720--------------------------------Process exited after 1.829 seconds with return value 0请按任意键继续. . .[Related recommendations:
C language video tutorial]
The above is the detailed content of How to calculate the factorial of n in C language. For more information, please follow other related articles on the PHP Chinese website!

VScode中怎么配置C语言环境?下面本篇文章给大家介绍一下VScode配置C语言环境的方法(超详细),希望对大家有所帮助!

在C语言中,node是用于定义链表结点的名称,通常在数据结构中用作结点的类型名,语法为“struct Node{...};”;结构和类在定义出名称以后,直接用该名称就可以定义对象,C语言中还存在“Node * a”和“Node* &a”。

c语言将数字转换成字符串的方法:1、ascii码操作,在原数字的基础上加“0x30”,语法“数字+0x30”,会存储数字对应的字符ascii码;2、使用itoa(),可以把整型数转换成字符串,语法“itoa(number1,string,数字);”;3、使用sprintf(),可以能够根据指定的需求,格式化内容,存储至指针指向的字符串。

C语言数组初始化的三种方式:1、在定义时直接赋值,语法“数据类型 arrayName[index] = {值};”;2、利用for循环初始化,语法“for (int i=0;i<3;i++) {arr[i] = i;}”;3、使用memset()函数初始化,语法“memset(arr, 0, sizeof(int) * 3)”。

c语言编译后生成“.OBJ”的二进制文件(目标文件)。在C语言中,源程序(.c文件)经过编译程序编译之后,会生成一个后缀为“.OBJ”的二进制文件(称为目标文件);最后还要由称为“连接程序”(Link)的软件,把此“.OBJ”文件与c语言提供的各种库函数连接在一起,生成一个后缀“.EXE”的可执行文件。

c语言计算n的阶乘的方法:1、通过for循环计算阶乘,代码如“for (i = 1; i <= n; i++){fact *= i;}”;2、通过while循环计算阶乘,代码如“while (i <= n){fact *= i;i++;}”;3、通过递归方式计算阶乘,代码如“ int Fact(int n){int res = n;if (n > 1)res...”。

区别:1、表示的含义不同,“*p”表示此指针指向的内存地址中存放的内容,“p”表示一个指针变量的名字,指此指针变量所指向的内存地址。2、输出的格式不同,“*p”输出的一般是一个和指针类型一致的变量或者常量,“p”输出的是一个16进制数, 输出一个指针的地址。3、功能不同,“*p”是让程序去那个地址取出数据,“p”用于存放的是地址。

go语言兼容c语言;Go语言可以调用C语言,还能给C语言调用。Go语言工具包中有一个Cgo命令,它用来处理Go调用C相关操作;而Go的函数可以导出给C用,只要在要导出的函数前面加上“//export funcname”就行了,然后可以使用“go build -buildmode=c-shared -o libxxx.so”命令编译生成动态库和头文件供C语言中使用。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 Linux new version
SublimeText3 Linux latest version

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 English version
Recommended: Win version, supports code prompts!
