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

#include<stdio.h>

int fact(int);

int main()
{
    int num;

    printf("Enter a positive number to find its Factorial\n");
    scanf("%d", &num);

    printf("\nFactorial of %d is %d.\n", num, fact(num));

    return 0;
}

int fact(int num)
{
    if(num)
        return(num * fact(num - 1));
    else
        return 1;
}

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:

num num * fact(num-1)
5 5 * fact(4)
4 4 * fact(3)
3 3 * fact(2)
2 2 * fact(1)
1 1 * fact(0)

Value Returning – Control Shifting back.

Function Return Value Result
1 * fact(0) return 1; 1 * 1 = 1
2 * fact(1) 1 2 * 1 = 2
3 * fact(2) 2 3 * 2 = 6
4 * fact(3) 6 4 * 6 = 24
5 * fact(4) 24 5 * 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

#include<stdio.h>

int fact(int);

int main()
{
    int num;

    printf("Enter a positive number to find its Factorial\n");
    scanf("%d", &num);

    printf("\nFactorial of %d is %d.\n", num, fact(num));

    return 0;
}

int fact(int num)
{
    return( (num > 0) ? (num * fact(num - 1)) : 1 );
}

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