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;
}
ParameterRecursion Iteration
Definition:The Recursion is a process in which a function calls itself and the corresponding function is known as Recursive functionIteration used a loop to execute a set of tasks.
Exit ConditionFunction exit after reaching base conditionIteration 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 SpaceRecursion consumes more memory because it uses the stack.Iteration use little memory
Code SizeThe code size is comparatively smaller.Code size is comparatively larger.
Out Of MemoryHighest Possible of out of memoryNo possible of out of memory
Logic ImplementationA simple algorithm is easy to implement but the complex is very to hard.Logic are easy to implement in complex code.
ApplicationFactorial , Fibonacci Series, graph, tree etc.Wide used
Example:void sumFunction(int a,int sum)
{
if(a > 10){
System.out.println(sum);
return;}
sum = sum + a;
a++;
sumFunction(a,sum);
}
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

Leave a Comment

Your email address will not be published. Required fields are marked *