# Recursion Vs Iteration In C/C++/Java, What you prefer?

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:

The Recursion is a process in which a function calls itself and the corresponding function is known as Recursive function. 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.

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

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

1. causate says:

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.

2. froleprotrem says:

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.

3. resources says:

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.

4. tonsillitis says:

Ι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.

5. I was reading through some of your blog posts on this site and I believe this web site is very informative ! Keep posting.