C Program to Find the Factorial of a Number Using Recursion

Introduction

The factorial of a non-negative integer ( n ) is the product of all positive integers less than or equal to ( n ). Factorials are commonly used in permutations, combinations, and other mathematical calculations. This guide will show you how to write a C program to find the factorial of a number using recursion.

Factorial Definition:

  • ( n! = n \times (n-1) \times (n-2) \times \dots \times 1 )
  • Special case: ( 0! = 1 )

Example:

  • Input: ( n = 5 )
  • Output: ( 5! = 120 ) (since ( 5 \times 4 \times 3 \times 2 \times 1 = 120 ))

Problem Statement

Create a C program that:

  • Takes a non-negative integer input from the user.
  • Uses a recursive function to calculate the factorial of the number.
  • Displays the factorial.

Solution Steps

  1. Include the Standard Input-Output Library: Use #include <stdio.h> for standard input-output functions.
  2. Write the Recursive Function: Define a recursive function that calculates the factorial of a number.
  3. Write the Main Function: Define the main function to take user input and call the recursive function.
  4. Input the Number: Use scanf to take input from the user for the number.
  5. Call the Recursive Function: Pass the input number to the recursive function to calculate the factorial.
  6. Display the Result: Use printf to display the factorial of the number.

C Program to Find the Factorial of a Number Using Recursion

#include <stdio.h>

// Step 2: Define the recursive function to calculate factorial
int factorial(int n) {
    if (n == 0) {
        return 1;  // Base case: 0! = 1
    } else {
        return n * factorial(n - 1);  // Recursive case: n! = n * (n-1)!
    }
}

int main() {
    // Step 3: Declare variables to hold the number and factorial result
    int num;
    int result;

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

    // Check for negative input
    if (num < 0) {
        printf("Factorial is not defined for negative numbers.\n");
    } else {
        // Step 5: Call the recursive function to calculate the factorial
        result = factorial(num);

        // Step 6: Display the result
        printf("Factorial of %d is %d\n", num, result);
    }

    return 0;  // Return 0 to indicate successful execution
}

Explanation

Step 2: Define the Recursive Function

  • The factorial function takes an integer n as input.
  • Base Case: If n is 0, the function returns 1 (since ( 0! = 1 )).
  • Recursive Case: If n is greater than 0, the function returns n * factorial(n - 1), which recursively calls itself with the value n - 1 until it reaches the base case.

Step 3: Declare Variables

  • The variable num stores the input number.
  • The variable result stores the factorial calculated by the recursive function.

Step 4: Input the Number

  • The program prompts the user to enter a non-negative integer using scanf.

Step 5: Call the Recursive Function

  • The program calls the factorial function with num as the argument and stores the result in result.

Step 6: Display the Result

  • The program uses printf to display the factorial of the input number.

Return 0

  • The return 0; statement indicates that the program executed successfully.

Output Example

Example 1:

Enter a non-negative integer: 5
Factorial of 5 is 120

Example 2:

Enter a non-negative integer: 0
Factorial of 0 is 1

Example 3:

Enter a non-negative integer: -3
Factorial is not defined for negative numbers.

Conclusion

This C program demonstrates how to calculate the factorial of a number using a recursive function. It covers basic concepts such as recursion, base cases, and user input, making it a useful example for beginners learning C programming.

Leave a Comment

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

Scroll to Top