BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

C Program to find factorial of a number using Recursion

Last Updated: May 19, 2024 by Chaitanya Singh | Filed Under: C Programs

In this guide, we will write a C Program to find factorial of a number using recursion. Recursion is a process in which a function calls itself in order to solve smaller instances of the same problem. This process continues until the smaller instance reaches a base case, at this post the recursion process stops and produces the result.

Key Concepts of recursion process

  1. Base Case: This is the condition at which the recursion process stops. Without a base case, the function would call itself indefinitely, leading to a stack overflow.
  2. Recursive Case: This part of the function calls itself with a modified argument, moving towards the base case.

C Program to find factorial of a number using recursion

The explanation of the program is available at the end of this code. The brief explanation of important lines is provided in the program itself using comments.

#include <stdio.h>

// A recursive function to calculate factorial
int factorial(int n) {
    if (n == 0) {
        return 1; // Base case
    } else {
        //function calling itself with modified argument
        return n * factorial(n - 1); 
    }
}

int main() {
    int num;

    // Prompt user to enter a number
    printf("Enter a non-negative integer: ");
    scanf("%d", &num); //store the number in num

    // Validate the input. Check whether the entered
    // number is non-negative.
    if (num < 0) {
        printf("Factorial is calculated for negative numbers.\n");
    } else {
        // Calculate and print the factorial for input number
        printf("Factorial of %d is %d\n", num, factorial(num));
    }

    return 0;
}

How Recursion Works in Factorial Example:

Base Case:

if (n == 0) {
return 1;
}

When n is 0, the function returns 1, stopping further recursive function calls.

Recursive Case:

return n * factorial(n - 1);

For a positive integer n, the function calls itself with the argument n - 1 and multiplies the result by n.

Example Output:

    Let’s say we call factorial(4):

    1. factorial(4) calls factorial(3)
    2. factorial(3) calls factorial(2).
    3. factorial(2) calls factorial(1).
    4. factorial(1) calls factorial(0).
    5. factorial(0) returns 1 (base case).
    6. factorial(1) returns 1 * 1 = 1.
    7. factorial(2) = 2 * factorial(1) = 2*1 = 2
    8. factorial(3) = 3 * factorial(2) = 3*2 = 6
    9. factorial(4) = 4 * factorial(3) = 4*6 = 24

    Top Related Articles:

    1. C Program to Search Substring in a given String
    2. C Program to find prime numbers in a given range
    3. C Program to concatenate two strings without using strcat
    4. C program to Reverse a String using recursion
    5. C Program to find largest element of an Array

    About the Author

    I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

    – Chaitanya

    Leave a Reply Cancel reply

    Your email address will not be published. Required fields are marked *

    Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap