Factorial program in C++ Using Recursion

Share on facebook
Share on twitter
Share on linkedin
Share on twitter
Share on tumblr

In this Basic C++ Program, we will have a look at how to write a c++ program to print factorial of a number using recursion.

C++ Recursion

When the function is called within the same function, it is known as recursion in C ++. The function that calls the same function is known as a recursive function. A function that calls itself and does not perform any task after a function call is called a queue recursion.

C++ Program Factorial Program using Recursion

#include<iostream>    

using namespace std;      

int main()    

{    

int factorial(int);    

int fact,value;    

cout<<"Please enter any number: ";    

cin>>value;    

fact=factorial(value);    

cout<<"Factorial of a number is: "<<fact<<endl;    

return 0;    

}    

int factorial(int n)    

{    

if(n<0)    

return(-1); /*Wrong value*/      

if(n==0)    

return(1);  /*Terminating condition*/    

else    

{    

return(n*factorial(n-1));        

}    

}  

Output:

Please enter any Number: 3
Factorial of 3 is: 6 

Leave a Comment

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