Ask for 5! and n!

When initially learning C language, 5! Use while loop to find. Methods as below:

681184652aec47ffa7062f71e39f9363.png

 Later, we learned how to call functions, and there was a new way to find the factorial of a number. Seek n! A recursive approach can be used.

We are right n! For analysis, it can be 0! =0,1! =0

, while the rest of the numbers are n! =n*(n-1)!

186f701022454030bf884747a2ce72a1.jpg

 So, we can write the following program:

012793d0aa7244e0afdd6d9447d859c5.png

 Notice:

The variable in the program is int type, the compiler system allocates 4 bytes for the int type data, and the maximum number that can be represented is 2 147 483 647. When n=12, the operation is normal, and the output is 479001600; if you input 13, try to find 13! , the expected result cannot be obtained, because the output result exceeds the maximum value of the int type data. The f, y, and fac functions can be defined as float type or double type.

 

Guess you like

Origin blog.csdn.net/m0_73939236/article/details/128168690