C Program to Find Factorial Using Recursion

Subject: PPS (Programming for Problem Solving)

Contributed By: Sanjay

Created At: February 3, 2025

Question:


Write a C Program to Find Factorial Using Recursion

Explanation Video:

Custom Image

Explanation:

 

  1. Factorial of n is the product of all numbers from 1 to n.

  2. The function factorial(n) calls itself recursively until it reaches n == 1.

  3. The base case is factorial(1) = 1.

Source Code:
#include <stdio.h>

int factorial(int n) {
    if (n == 0)
        return 1;
    else
        return n * factorial(n - 1);
}

int main() {
    int num;
    printf("Enter a number: ");
    scanf("%d", &num);
    printf("Factorial of %d is %d\n", num, factorial(num));
    return 0;
}
Input:
Enter a number: 5
Output:
Factorial of 5 is 120
Share this Article & Support Us:
Status
printf('Loading...');