C++ Program to find the Factorial using recursive function




Online computer science courses to jumpstart your future.

#include<iostream>
using namespace std;

int Factorial(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*Factorial(n-1);
        }
}

int main(){

    cout<<"Enter to number to find Factorial: ";
    int num;
    cin>>num;
    num=Factorial(num); // function call
    cout<<"\nFactorial of number is: "<<num << endl;
return 0;
}

/*
OUTPUT
Enter to number to find Factorial: 5                                                                                                                                            
1 : time                                                                                                                                                                        
2 : time                                                                                                                                                                        
3 : time                                                                                                                                                                        
4 : time                                                                                                                                                                        
5 : time                                                                                                                                                                        
                                                                                                                                                                                
Factorial of number is: 120 
 
*/

Partner Sites

VideoToGifs.com

EasyOnlineConverter.com

SqliteTutorials.com





Be the first to comment

Leave a Reply

Your email address will not be published.


*