When initially learning C language, 5! Use while loop to find. Methods as below:
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)!
So, we can write the following program:
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.