Tuesday, June 16, 2020

Factorial of a number using recursion in C++

#include<iostream> 

using namespace std;

int factorial (int a){

if(a > 1){
return a * factorial(a-1);
}
else
return 1;
}

int main(){
  int n;
  cin>>n;
  cout<<"The factorial of "<<n<<" is "<<factorial(n);
}

No comments:

Post a Comment

horizontal ads