Find factorial using recursion in C++

Here we will write a C++ program to find factorial using recursion, first we will understand what are factorials of a number and how to find out the factorials of a number.

Lets start with what is factorial.

What is Factorial of a number:

The factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n. For example,

Factorial of 5,   5!= 5 x 4 x 3 x 2 x 1 = 120

Now we will write a program to find factorial using recursion in C++.

 

C++ Program to find factorial using recursion:

#include<iostream>
using namespace std;

int recursive_function(int n){
static int i=1;// to make one time initialization
cout<<i<<" : time"<<endl; // counting the function calls

if(n==1){
return 1;//base case
}
else
i++;
return n=n*recursive_function(n-1);
}

int main()
{
cout<<"Enter to number to find Factorial: ";
int num;
cin>>num;
num=recursive_function(num); // function call
cout<<"\n\n\t\tFactorial of number is: "<<num;
return 0;
}

 

OUTPUT:

find factorial using recursion in C++

Leave a Comment