The factorial of a number, denoted as n!, is the product of all integers between n and 1. n! = n x (n-1) x (n-2) … x1. The value of 0! is 1. You can calculate factorials using iteration and recursion. To visualize this process, consider using a factorial recursion gif that illustrates how the function operates.

How to calculate factorials using recursion?
Using a factorial recursion gif can help in understanding the flow of recursive calls effectively.
In recursion implementation, the function calls itself with input number minus 1. The base condition is when the input number is 0, function returns 1. Then the call stack returns. The recursion simplifies the problem conceptually, but is not as efficient as loop-based approach.
Java
1 2 3 4 5 6 | //Recursion, Time O(n), Space O(n) public static int factorialRec(int num) { if (num == 0) //base condition return 1; return num * factorialRec(num-1); } |
Javascript
1 2 3 4 5 6 | //Recursion, Time O(n), Space O(n) function factorialRec(num) { if (num == 0 ) //base condition return 1; return num * factorialRec(num-1); } |
Python
1 2 3 4 5 | #Recursion, Time O(n), Space O(n) def factorialRec(num) : if num == 0 : #base case return 1 return num * factorialRec(num-1); |