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

// 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;
}

Sum of Digits using Recursion

//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;
}

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++

//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;
}

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.

#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;
}

LCM of Two Numbers using recursion

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;
}

GCD of Two numbers using recursion

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;
}

Decimal to Binary Conversion Using Recursion

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;
}

Largest Number from an array Using Recursion

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

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;
}

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.

int isPrime(int num,int i){

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

Sum of digits of any given number using recursion

#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;
}

Recursive function to find out reverse of any number

#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;
}

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




Suggested Similar Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>