# 30 Best Recursive Programs in C++ Recursion is the process when a function can call itself without completing its definition. One of the best known example of recursion is – Find out the factorial of any number N  but beyond that not too many recursive function are available on the net. Thus tried to provide so many recursive function to do some simple task.

Note :Please note recursion use Memory Stack to perform all these operations

So today we are going to show you the code of some of the simplest task using recursion.

### Print first  N fabonacci number using recursion

[code]
// recursive function to print first N fabonacci numbers
#include
#include
using namespace std;
int fab(int N){
if(N==1)
return 0;
else
if(N==2)
return 1;
else
return(fab(N-2)+fab(N-1));
}

int main(){
for(int i=1;i<=20;i++)
cout<<setw(6)<<fab(i);
return 0;
}

[/code]

### Sum of Digits using Recursion

[code]
//sum of digit usign recursion
#include<iostream>
using namespace std;
int sum(int n)
{
if(n==0)
return 0;
else
return (n%10+sum(n/10));
}

int main(){
cout<<"\n Sum of digits :"<<sum(1134);
return 0;
}
[/code]

### Factorial of any Number N using recursion

This is one of the classical example of recursion and almost available on everywhere still shared this to show how you can implement the same using C++

[code]
//factorial of any number N
#include<iostream>
using namespace std;
int fact(int n)
{
if(n==1)
return 1;
else
return n*fact(n-1);
}

int main(){
cout<<"\n Factorial of "<<5<<" is :"<<fact(5);
return 0;
}
[/code]

### Print first 100 Natural Numbers using recursion

These type of programs are best denoted using for loop or do while loop but here we have taken this example to show how you can do the simple task using recursion.

[code]
#include<iomanip>
#include<iostream>
using namespace std;
void print(int n)
{
static int i=0;
if(n==0)
return;
else
{
i++;
cout<<setw(4)<<i;
print(n-1);
}
}
int main(){
print(100);
return 0;
}
[/code]

### LCM of Two Numbers using recursion

[code]
int lcm(int a,int b){
static int temp = 1;

if(temp % b == 0 && temp % a == 0)
return temp;
temp++;
lcm(a,b);

return temp;
}
[/code]

### GCD of Two numbers using recursion

[code]
int findgcd(int x,int y){
while(x!=y){
if(x>y)
return findgcd(x-y,y);
else
return findgcd(x,y-x);
}
return x;
}
[/code]

### Decimal to Binary Conversion Using Recursion

[code]
long toBinary(int decimalNo){

static long binaryNo,remainder,factor = 1;

if(decimalNo != 0){

remainder = decimalNo % 2;
binaryNo = binaryNo + remainder * factor;
factor = factor * 10;
toBinary(decimalNo / 2);
}

return binaryNo;
}
[/code]

### Largest Number from an array Using Recursion

This recursive function find out the largest number among an array of integer type elements.

[code]
int getMaxElement(int arr[]){

static int i=0,max =-9999;
if(i < size){
if(max<arr[i])
max=arr[i];
i++;
getMaxElement(arr);
}
return max;
}
[/code]

### Prime Number using recursion

This is recusive version to check whether the entered number is prime or not. The function return 1 is prime otherwise the function return 0.

[code]
int isPrime(int num,int i){

if(i==1){
return 1;
}else{
if(num%i==0)
return 0;
else
isPrime(num,i-1);
}
}
[/code]

### Sum of digits of any given number using recursion

[code]
#include<iostream>
#include<iomanip>
using namespace std;
int sum(int n)
{
if(n==0)
return 0;
else
return (n%10+sum(n/10));
}

int main(){
cout<<"\n Sum of digits (12345) :"<<sum(12345);
return 0;
}
[/code]

### Recursive function to find out reverse of any number

[code]
#include<iostream>
#include<iomanip>
using namespace std;
long reverse(int N){
static long rev_no,remainder;

if(N != 0){

remainder = N % 10;
rev_no = rev_no*10 + remainder;
reverse(N / 10);
}

return rev_no;
}
int main()
{
cout<<"\n Reverse of 4321 is :"<<reverse(4321);
return 0;
}
[/code]

These are our first 10 Best recursive programs for all you out there. Next batch of 30 Best recursive programs will cover more topics like binary search, selection search and advance data structure. keep in touch