In this post, I am going to discuss the basic difference between ** Recursion vs Iteration** In C/c++/Java. Recursion or iteration both is able to do the task in their own way. Many advanced coders always prefer Recursion Over Iteration. So, without wasting time let’s come on the differences.

*Recursion:*

*Recursion:*

The ** Recursion** is a process in which a function calls itself and the corresponding function is known as

**Recursion is widely used in Competitive programming, Interview problems, and in real life. Some of the famous problem done using recursion is Tree traversal, Tower of Hanoi, Graph, etc.**

*Recursive function.**Example Of Recursion:*

*Example Of Recursion:*

Below is the recursive function In java for adding numbers from 1 to 10. sumFunction is a recursive function

```
/*package whatever //do not write package name here */
import java.io.*;
class GFG {
static void sumFunction(int a,int sum)
{
if(a > 10){
System.out.println(sum);
return;}
sum = sum + a;
a++;
sumFunction(a,sum);
}
public static void main (String[] args) {
sumFunction(1,0);
}
}
```

## Iteration:

Iteration used a loop to execute a set of tasks. Novice programmers prefer Iteration over Recursion.

### Example

Program to Add number from 1 to 10 using for loop

```
void sumUsingLoop()
{
int sum = 0;
for(int i = 0;i<10;i++)
sum = sum + i;
cout<<sum<<endl;
}
```

Parameter | Recursion | Iteration |

Definition: | The is a process in which a function calls itself and the corresponding function is known as RecursionRecursive function | Iteration used a loop to execute a set of tasks. |

Exit Condition | Function exit after reaching base condition | Iteration exit after the condition placed in loop |

Performance | recursion works in a stacked manner that’s why performance is slow. | Since, Iteration does not need initializing variable again and again, It’s performance is fast |

Memory Space | Recursion consumes more memory because it uses the stack. | Iteration use little memory |

Code Size | The code size is comparatively smaller. | Code size is comparatively larger. |

Out Of Memory | Highest Possible of out of memory | No possible of out of memory |

Logic Implementation | A simple algorithm is easy to implement but the complex is very to hard. | Logic are easy to implement in complex code. |

Application | Factorial , Fibonacci Series, graph, tree etc. | Wide used |

Example: | `void sumFunction(int a,int sum)` | `void sumUsingLoop()` |

## Problem

### Question 1:

Which of the given following problems can not be solved using recursion technique?

a) Nth Fibonacci

b) Factorial of Number

c) Length of a string

d) Problems without base case

*Answer – d : If a problem does not have base case, recursion leads to infinite calling*

### Question 2:

The ___________ condition is used to stop the recursive function

a) Best case

b) Worst case

c) Base case

d) There is no such condition

*Answer – c: Base case is used to stop the recursive function*

### Question 3:

Predict The Output

```
void my_recursive_function(int n)
{
if(n == 0)
return;
my_recursive_function(n-1);
printf("%d ",n);
}
int main()
{
my_recursive_function(10);
return 0;
}
```

It will print numbers from 1 to 10

Hope you will understand the basic difference between *Recursion vs Iteration*

It’s very eɑsy to find out any topic on net aѕ compared to books,

as I found thіs post at this weƄ site.

Does your blog have a contact page? I’m having trouble locating it but, I’d like to send you an e-mail. I’ve got some suggestions for your blog you might be interested in hearing. Either way, great blog and I look forward to seeing it develop over time.

I am in fact pⅼeased to glance at thiѕ blog posts which carrіes lotѕ of helpful factѕ, thаnks for providing these kinds of data.

Ιt’s impresѕive thаt you are gettіng ideas from this piece of writing as weⅼl as from our discussion maⅾe hеre.