Types and examples of Recursion in C++

Types and examples of Recursion in C++
Techiio-author
Written by Sagar RabidasFebruary 11, 2022
7 min read
C++
1 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 1 VIEWS SHARE
Techiio-author
Sagar Rabidas

Software Developer

In this blog, we will discuss Types and examples of Recursion in C++

What is Recursion in C++?

Recursion in c ++ manner growing a loop to carry out a procedure repetitively to finish a selected venture. Consequently, any feature that calls itself again and again in code is called a recursive characteristic. C ++ recursion is the most efficient and powerful manner of fixing large and complex mathematical problems by dividing them into smaller obligations small traces of code. The approach is also referred to as divide and overcome. The syntax for the Recursive function in C ++ is given below:

Syntax:

void recursive_function() // Name of the recursive function
{
// Here is the function the will calls itself
recursive_function() ;
}
int main ()
{
recursive_function() ;
return 0 ;
}

To understand how recursion works in C ++ let us see through a C ++ code that will help us understand step by step working of recursion in C++. But before that, we will discuss types of recursion in C ++ programming.

Types of Recursion in C++

There are two types of recursion:

  1. Direct Recursion
  2. Indirect Recursion

Direct Recursion

When a function call itself directly, means it’s a direct recursive function. In the below syntax, you can see we have defined a function with the name recursive_function(). After that, we are calling the same recursive_function() inside recursive_fucntion(). This is the way of using direct recursion in your code.

Syntax:

void recursive_function()
{
recursive_function();
}

Indirect Recursion

When a function calls itself indirectly, it means it’s calling the function with the help of another function defined is called indirect recursion. In the below syntax, you can see we have defined a function with name function () and inside that, we have defined a recursive_function(). After that, we are calling the function () inside recursive_fucntion(). This is the way of using indirect recursion in your code.

Syntax:

void function ()
{
recursive_function () ;
}
void recursive_function ()
{
function () ;
}

Examples of Recursion in C++

Below are the examples of Recursion in C++.

Example #1

Here is the C + code to demonstrate the working of recursive function in the C ++ programming language:

Code:

#include<iostream>
using namespace std;
int main ()
{
int factorial_num ( int ) ;
int facto, value ;
cout << " Please enter any number to find factorial of it : " ;
cin >> value ;
facto = factorial_num ( value ) ;
cout << " The Factorial of given number is: " << facto << endl ;
return 0 ;
}
int factorial_num ( int n )
{
if ( n<0 )
return ( -1 ) ;
if ( n == 0 )
return ( 1 ) ;
else
{
return ( n*factorial_num (n-1) ) ;
}
}

Example #2

Here is the C + + code to demonstrate the working of a recursive function and iterative function together in a single C + + programming language:

Code:

#include <iostream>
using namespace std ;
int factorial_fact ( int n )
{
if( n == 0 || n == 1 )
return 1 ;
return n * factorial_fact ( n-1 ) ;
}
int factorial_iterator ( int n )
{
int fact = 1 ;
for ( int i = n; i > 1; i--) {
fact *=  i ;
}
return fact ;
}
int f[100] = {0} ;
int factorial_dynamic ( int n )
{
if  (f[n] ) return f[n] ;
if ( n == 0 || n == 1 ) {
f[n] = 1 ;
return f[n] ;
}
f[n] = n*factorial_fact ( n-1 ) ;
return f[n] ;
}
int main()
{
cout << " The recursive factorial of number 15! = " << factorial_fact ( 15 ) << endl ;
cout << " The iterative factorial of the number 15! = " << factorial_iterator ( 15 ) << endl ;
}

C
C++
Recursion in C++
Recursion
Recursion function
1 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 1 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance