The Blog

What Is Recursion In C++?

What Is Recursion In C++?

Have you ever heard of the term recursion? Do you want to know what is meant by recursion in C++?

Today, we are going to talk about what is meant by recursion in C++. We will also see how to find the factorial of a number using recursion in C++.

So, let’s get started!

What is recursion?

So, what is recursion in general? 

If we type ‘recursion’ on our browser and hit search, it will give us the result that recursion is ‘the repeated application of a recursive procedure or definition.’

In computer science, this definition holds true as well. Would you like to know what is meant by recursion in C++?

What is recursion in C++?

What is Recursion in C++? Let’s get to know!

 

We use recursion in C++ mainly with functions. In simple words, when a function calls itself, the process is called recursion.

Moreover, the function that calls itself in its own body is said to be a recursive function.

In other words, recursion happens when a statement in the body of the function calls the function.

Furthermore, it should be noted that the function should have a terminating condition; also known as the base condition in its body.

If the terminating or the base condition is not present in the recursive function, the function will be invoked endlessly or enter an infinite loop.

This should be avoided. Hence, while working with recursion in C++, make sure to pay attention to the terminating condition.

Now, let us understand what is recursion in C++ with the help of an example.

 

Recursion in C++ – Example code.

 

We have an example program to help you understand what is recursion in C++ in a better way.

In the following example, to know what is meant by recursion in C++, we will see how to find the factorial of a number using recursion.

 

How to find the factorial of a number using recursion in C++?

 

Let us see how to find the factorial of a number using recursion with the code below.

 

Steps to find the factorial of a number using recursion:

 

  1. Input the number you want to find the factorial of.
  2. Call the function factorial() to find the factorial for the number the user has given as input.
  3. In the function factorial(), specify the base condition.
  4. The base condition in the function is the if statement. When the value of n will be equal to one, the recursive call will be terminated.
  5. To find the factorial of the number, recursively call the function factorial() using n*factorial(n-1).
  6. The function will then return the value of the factorial to the main function.
  7. Print the factorial.

 

C++ code to find the factorial of a number using recursion.

 

#include<iostream>

using namespace std;




// factorial() is a recursive function

int factorial(int n){

    if(n == 1)                      // base condition

    return 1;

    else

    return n*factorial(n-1);        // calling the function recursively

}




int main(){

    int n, Fact;

    cout<<"\t\t\t FACTORIAL USING RECURSION \n\n";

    cout<<"Enter the number you want to find the factorial of: ";

    cin>>n;     //input the number

    if(n < 1)

    cout<<"Please enter a natural number.";

    Fact = factorial(n);    // calling the function

    cout<<"Factorial of "<<n<<" is "<<Fact;     //printing the result

    return 0;

}

 

Output

 

Output- Factorial using recursion

Suppose, the number you want to find the factorial of is 5. 

The function factorial() calls itself to find the factorial of (n-1), which is 4 in this case, and returns 5*factorial(4).

The function again invokes itself and finds the factorial of (n-1) which now becomes 3 and returns 4*factorial(3) to the previously called function.

The process is repeated till the value of n becomes 1. 

Thus, the result of factorial(1) is returned to factorial(2) which further returns the result to factorial(3) and so on.

Hence, when the function is terminated, we are able to find the factorial of 5 which is 120.

This is how recursion occurs in C++.

I hope now you know what is recursion in C++?

 

If  you need help with your C++ programming homework  don’t worry! Visit https://codingzap.com/ and our expert team will be there to help you out.

We also offer a wide range of programming and coding help services for you to benefit from. Don’t miss out!

Checkout our other listed services as well!

Computer science Homework Help

Java Homework Help 

C Programming Homework Help

Python Homework Help

PHP Homework Help

 

So, go ahead and give it a try yourself!

 

Sanjana Grover
Sanjana Grover

Leave a Comment