How Recursion Works?

 

#include <bits/stdc++.h>
using namespace std ;

int fact(int n){
//1st step Base Case
if (n==0){
return 1 ;
}
int smallans = fact(n-1) ; // 2nd step Assumption Recursive Case

int ans = n*smallans ; //3rd step Calculation
return ans ;
}
int main(){
int n ;
cin >> n ;
cout << fact(n) << '\n';


return 0 ;
}

Comments

Popular posts from this blog

CodeChef::CSUB

CodeChef::TREE2