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

This is a C++ Program to find the factorial of a number using recursion.

Problem Description

We have to write a C++ program to find out the factorial of a given number using recursion. In mathematics, the factorial of a positive integer n, denoted by n!, is the product of all positive integers less than or equal to n.
For example,
5! = 5 * 4 * 3 * 2 * 1 = 120
The value of 0! is 1, according to the convention for an empty product.
NOTE: Factorial is calculated only for non negative integers.

Expected Input and Output

Case 1. When we take a positive integer

If the input number is 5,
then the expected output will be 120.

Case 2. When the number is zero.

If the input number is 0,
then the expected output will be 1.
Problem Solution

1. In order to find factorial of a number we can either create a separate function or calculate in the main function itself.
2. First of all input the number whose factorial is to be determined (say n).
3. If the number is 0 or 1 simply return 1.
4. If the number is other than 0 or 1 (say n), then recursively call factorial function as: n*factorial(n-1), where factorial() is the function which returns the factorial of a number.
8. This way we calculate n! as n*(n-1)*(n-2)*(n-3)……..1

Program/Source Code

Here is source code of the C++ Program to find the factorial of a number using recursion. The program is successfully compiled and tested using Codeblocks gnu/gcc compiler on Windows 10. The program output is also shown below.

  1. /*
  2.  * C++ Program to find factorial of a number using recursion.
  3.  */
  4. #include<iostream>
  5. using namespace std;
  6. class fact
  7. {
  8. public:
  9.     /*
  10.      * Function to find the factorial
  11.      */
  12.     int factorial(int n)
  13.     {
  14.         int pro = 1;
  15.         if(n == 0 || n == 1)
  16.         {
  17.             return 1;
  18.         }
  19.         return n * factorial(n-1);
  20.     }
  21. };
  22. /*
  23.  * Main function
  24.  */
  25. int main()
  26. {
  27.     fact f;
  28.     int n;
  29.     cout<<"Enter the number whose factorial is to be calculated\t";
  30.     cin>>n;
  31.     cout<<"\nFactorial of "<<n<<" is = "<<f.factorial(n);
  32.     return 0;
  33. }
Program Explanation

1. Here in this program we have created a function called factorial which takes in the number whose factorial is to be determined.
2. We will recursively call the function factorial till n keeps on decreasing to become 1.
3. Since at every recursion we are passing factorial(n-1) after some time this n-1 will become equivalent to 1 which can lead to a terminating case where if n is either 0 or 1, we return 1.
So this way we calculate factorial recursively as n * factorial(n – 1).

Note: Join free Sanfoundry classes at Telegram or Youtube
Runtime Test Cases
1. Enter the number whose factorial is to be calculated    5
   Factorial of 5 is = 120
2. Enter the number whose factorial is to be calculated    0
   Factorial of 0 is = 1

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

If you wish to look at all C++ Programming examples, go to C++ Programs.

If you find any mistake above, kindly email to [email protected]

Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.