the uses of recursion in c++ code example
Example 1: recursion in c++
//AUTHOR:praveen
//Function calling itself
//Example in c++
#include<iostream>
using namespace std;
int recursion(int a){
if(a==1)//BASE CASE
return 0;
cout<<a;
a=a-1;
return recursion(a);//FUNCTION CALLING ITSELF
}
int main(){
int a=5;
recursion(a);
return 0;
}
//OUTPUT: 5 4 3 2
Example 2: recursion in c++
//recursion in c++
//factorial
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int factorialfun(int num)
{
if (num>0)
{
return num*factorialfun(num-1);
}
else
{
return 1;
}
}
int main()
{
int num;
cin>>num;
cout<<factorialfun(num);
}