Introduction to Recursion

 

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

int fact(int n){
if (n < 0) return -1 ;
if (n==0) return 1;
int small_ans = fact(n-1);
return n*small_ans ;

}

int main (){
int n ;
cin >> n ;

int ans = fact(n);
cout << ans << '\n';
return 0;
}

Comments

Popular posts from this blog

CodeChef::CSUB

How Recursion Works?

CodeChef::TREE2