C/C++

# Recursion In c/c++ And How To Use Recursion Function Factorial program ## Description:

Recursion in c/c++-this is a very detailed tutorial in which we will learn what is recursion and how to recursion function to find the factorial of a number

without any further delay, let’s get started

## What is recursion in c/c++:

function that calls itself is known as a recursive function and this technique is known as recursion so recursion in c/c++ is basically the process of rebuilding items in a similar way and in terms of programming especially recursive functions the function gives a call to itself based on certain criteria so this enables the function to repeat itself several times outputting the result at the end of each iteration kind of like a for loop or any other looping control structure and the recursion continues until some condition is met so that it prevents infinite recursion right so in even in for loop while loop we have a condition wherein the for the control is exited outside the loop similarly in recursive functions there has to be a condition after which the recursion should stop so sometimes if-else condition is used inside okay so this was a little bit about recursive functions.

## Example: calculate the sum of first n natural numbers so natural numbers start from 1 to infinity so I want to calculate the sum of first three numbers

```#include<iostream>
using namespace std;
int sum(int num)
{
if(num !=0)
return (num + sum(num-1));
else
return num;
}
int main()
{
int n;
cout<<”enter number:  ”;
cin>>n;
int total =  sum(n);
cout<<endl<<”the sum is: ”<<n<<total<<endl;
return 0;
}
```

### Program explanation of recursion in c/c++:

you can see on line number 3 we have the signature of prototype of the function you can see return type is int sum is the name of the function and it is taking one integer value so this is the upper limit of the number of numbers that you want to calculate the sum off so if you want to calculate from one two three you enter three so inside the function you can see it’s saying if num not equal to zero that is if the number is not equal to zero then you have to return num plus sum of num minus 1 so you can see in yellow the sum is called inside the sum function itself so this is a recursive call that is inside the sum function and calling again the sum function with a new argument which is one less than what the number we first and if the num is equal to zero then return that number as it is so this is that if else condition which prevents the infinite recursion in c/c++programming and in the main function what I’m doing is I’m declaring a integer variable int n I am saying enter number till which you want to tell you till where you want the sum of natural numbers to be calculated then I take the input from the user and then I create one more integer variable int total  I’ll say in total and say sum and I pass that number so if the user enters 3 I pass 3 over the function and since the  recursive function returns an integer value you can see total equal to sum so the integer value returned would be assigned to the total integer variable that’s how return types work right so in the end I say the sum is and the number which entered by the user  and I print out the total.

## The difference between iteration and recursion in c/c++

 Recursion Iteration Function calls itself Functions loops some part of code Recursion is a selection structure Iteration is a repetition structure Recursion achieves repetition through repeated method calls Iteration explicitly uses repetition structures Recursion terminate when a base case is recognized Iteration terminates when the loop continuation condition fails Recursion invokes the function repeatedly and  hence the overhead of method calls can be costly in both processing time and memory space Iteration repeatedly executes code this can be less expensive in both processor time and memory space Infinite recursion occurs if the recursion step does not reduce the  problem in a manner that converges on the base case An infinite  loop occurs with iteration it the loop continuation test never become false Recursion returns value to the calling function Iteration does not return value Recursion makes code smaller Iteration makes the code longer Infinite recursion can crash the system Infinite looping uses CPU cycles repeatedly Recursion is a slower process Iteration is faster.

## Example: write a program which finds the factorial of a number using recursion function

```#include<iostream.h>
#include<conio.h>
void main()
{
int a;
cout<<”Enter the number :”;
cin>>a;
cout<<”Factorial = ”<<fact(a);
getch();
}
long int fact(int n)
{
if(n==0)
return(1);
else
return(n * fact(n-1));
}
```

### Program explanation of recursion in c/c++:

we write a function fact which in turn called the same function because during the calculation of factorial we can say that n factorial is equals n into n minus 1 factorial for defining the function we are using a function name as fact which takes an integer parameter as n and returns a result as a long value which will be the factorial value of n in the function definition we are checking whether the value of n is zero so if the value of n is zero we return the result as 1 because 0 factorial is 1 as if the value of n is more than 0 we calculate the result as n into factorial of n minus 1 and that value will be returned so this is an example for a recursion in c /c++ function.