A factorial is a number multiplied by all the positive integers which are below itself. For example, the factorial of 4 is:
4! = 4x3x2x1
This multiplication can be done in program with the help of recursion:
4! = 4x3x2x1
This multiplication can be done in program with the help of recursion:
factorial() is a function which calculates the factorial of the number. B
We can look at it backwards:
factorial(1) = 1. factorial(1) is the last process the program makes.
Then the program runs factorial(2). this process runs 2*factorial(1) which is 2*1.
Then factorial(3). factorial(3) is 3*factorial(2), so 3*2*1
factorial(4) = 4*factorial(3). factorial(3) is 3*2*1 so factorial(4) = 4*3*2*1
It does the factorial of 5 to give a final result
We can look at it backwards:
factorial(1) = 1. factorial(1) is the last process the program makes.
Then the program runs factorial(2). this process runs 2*factorial(1) which is 2*1.
Then factorial(3). factorial(3) is 3*factorial(2), so 3*2*1
factorial(4) = 4*factorial(3). factorial(3) is 3*2*1 so factorial(4) = 4*3*2*1
It does the factorial of 5 to give a final result