C Program To Find Factorial of a Number using Recursion

Lets write a C program to find Factorial of a user input number using Recursion.

Factorial Definition: Factorial of a positive integer n, denoted by n!, is the product of all positive integers less than or equal to n.

Important Note: By convention, Factorial of 0 is 1. i.e., 0! = 1.

Example: 5! = 5 x 4 x 3 x 2 x 1 which is equal to 120. i.e., 5! = 120.

Formula To Calculate Factorial of any positive integer number
We can calculate factorial of any number using this relationship:

num! = num * (num – 1)!

where num is a positive integer number.

Related Read:
C Program To Find Factorial of a Number
Recursive Functions In C Programming Language

Video Tutorial: C Program To Find Factorial of a Number using Recursion



YouTube Link: https://www.youtube.com/watch?v=efcK6uNZzKw [Watch the Video In Full Screen.]


Source Code: C Program To Find Factorial of a Number using Recursion

  1. #include<stdio.h>  
  2.   
  3. int fact(int);  
  4.   
  5. int main()  
  6. {  
  7.     int num;  
  8.   
  9.     printf("Enter a positive number to find its Factorial\n");  
  10.     scanf("%d", &num);  
  11.   
  12.     printf("\nFactorial of %d is %d.\n", num, fact(num));  
  13.   
  14.     return 0;  
  15. }  
  16.   
  17. int fact(int num)  
  18. {  
  19.     if(num)  
  20.         return(num * fact(num - 1));  
  21.     else  
  22.         return 1;  
  23. }  

Output 1:
Enter a positive number to find its Factorial
7

Factorial of 7 is 5040.

Output 2:
Enter a positive number to find its Factorial
8

Factorial of 8 is 40320.

Logic To Find Factorial of a Number using Recursion

We ask the user to enter a positive integer number and we pass this number to a function called fact().

Inside fact() function
Inside fact() function, we check if the number is non-zero, if true, we execute the code inside if block orelse(if num is zero), then the code inside else block gets executed.

Inside if block, we add the factorial of (num-1) to the value of num.

num * fact(num – 1)

and return the result to the calling function.

Inside else block, we simply return 1. Because factorial of 0 is 1. So when num is 0, we return 1.

Example:

numnum * fact(num-1)
55 * fact(4)
44 * fact(3)
33 * fact(2)
22 * fact(1)
11 * fact(0)

Value Returning – Control Shifting back.

FunctionReturn Value Result
1 * fact(0)return 1;1 * 1 = 1
2 * fact(1)12 * 1 = 2
3 * fact(2)23 * 2 = 6
4 * fact(3)64 * 6 = 24
5 * fact(4)245 * 24 = 120

Finally 120 will be returned to main() method where we call the fact() method.

Source Code: C Program To Find Factorial of a Number using Recursion and Ternary or Conditional Operator

  1. #include<stdio.h>  
  2.   
  3. int fact(int);  
  4.   
  5. int main()  
  6. {  
  7.     int num;  
  8.   
  9.     printf("Enter a positive number to find its Factorial\n");  
  10.     scanf("%d", &num);  
  11.   
  12.     printf("\nFactorial of %d is %d.\n", num, fact(num));  
  13.   
  14.     return 0;  
  15. }  
  16.   
  17. int fact(int num)  
  18. {  
  19.     return( (num > 0) ? (num * fact(num - 1)) : 1 );  
  20. }  

Output 1:
Enter a positive number to find its Factorial
5

Factorial of 5 is 120.

Output 2:
Enter a positive number to find its Factorial
6

Factorial of 6 is 720.

Know more about ternary operator or conditional operator, watch a separate video tutorial: Ternary Operator / Conditional Operator In C.

Memory Stack

Whenever there is a recursive function call, function instance and the memory associated with that function instance gets pushed into the stack. When any function instance returns a value, that function instance and memory associated with that function instance gets popped out or removed or gets deleted from the memory stack.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert