C++ Program to Find the Factorial of a Number Using Recursion

Finding the factorial of a number is a common problem in mathematics and computer science. The factorial of a non-negative integer n, denoted as n! which is the product of all positive integers less than or equal to n. Factorials are used in various applications, including combinatorics, algebra, and calculus.

One of the elegant ways to calculate the factorial of a number is by using recursion. In this article, we will explore the concept of recursion and demonstrate how to use it to find the factorial of a number in C++.

Prerequisites

Before diving into the example, it’s essential to have a basic understanding of:

  • Functions and their syntax in C++
  • The concept of recursion
  • Basic input and output operations in C++
  • Compiling and running a C++ program

Understanding Recursion

Recursion is a technique where a function calls itself to solve smaller instances of the same problem. A recursive function has two main components:

  1. Base Case: The condition under which the recursion ends.
  2. Recursive Case: The part of the function where it calls itself with a smaller or simpler subproblem.

In the case of finding a factorial, the base case is when nnn is 0 or 1, as 0!0!0! and 1!1!1! are both 1. The recursive case involves multiplying nnn by the factorial of n−1n-1n−1.

C++ Program to Find the Factorial of a Number Using Recursion

C++
#include <iostream>
using namespace std;

// Recursive function to find factorial of a number
int factorial(int n) {
    if (n <= 1) { // Base case
        return 1;
    } else {
        return n * factorial(n - 1); // Recursive case
    }
}

int main() {
    int num;
    cout << "Enter a number: ";
    cin >> num;
    
    if (num < 0) {
        cout << "Factorial of a negative number is undefined." << endl;
    } else {
        cout << "Factorial of " << num << " is " << factorial(num) << endl;
    }
    
    return 0;
}

Output

C++
Enter a number: 5
Factorial of 5 is 120

Explanation:

  1. Function Definition:
    • The factorial function takes an integer n as its parameter.
    • The base case checks if n is less than or equal to 1. If true, it returns 1.
    • The recursive case returns n multiplied by the factorial of n-1.
  2. Main Function:
    • The program prompts the user to enter a number.
    • It reads the input number.
    • It checks if the number is negative, as the factorial of a negative number is undefined.
    • If the number is non-negative, it calls the factorial function and prints the result.

Example 1: Finding Factorial of 4

C++
Enter a number: 4
Factorial of 4 is 24

Explanation:

  • The function calls proceed as follows: factorial(4), factorial(3), factorial(2), factorial(1).
  • The results are: 1! = 1, 2! = 2 * 1 = 2, 3! = 3 * 2 = 6, 4! = 4 * 6 = 24.

Example 2: Finding Factorial of 0

C++
Enter a number: 0
Factorial of 0 is 1

Explanation:

  • The base case is met immediately, and the function returns 1.

Conclusion

Recursion provides an elegant solution for calculating the factorial of a number. By breaking down the problem into smaller subproblems, recursive functions can handle complex tasks with concise code. However, it’s important to ensure that a base case is defined to prevent infinite recursion.

This article demonstrated how to find the factorial of a number using recursion in C++, providing a clear understanding of the concept and practical implementation. Recursion is a powerful tool in a programmer’s toolkit, and mastering it can greatly enhance your problem-solving abilities in C++.

4o

ChatGPT can make mistakes. Check importa