Difference Between Recursion and Iteration

Difference Between Recursion and Iteration in Tabular Form

The concept of Recursion and Iteration is to execute a set of instructions repeatedly. The key difference between recursion and iteration is that recursion is a process to call a function within the same function while iteration is to execute a set of instructions repeatedly until the given condition is true.


Comparison Chart

RECURSIONITERATION
Recursion is like piling all of those steps on top of each other and then quashing them all into the
solution
In iteration, a problem is converted into a train of steps that are finished one at a time, one after
another
In recursion, each step replicates itself at a smaller scale, so that all of them combined together
eventually, solve the problem.
With iteration, each step clearly leads onto the next, like stepping stones across a river
Not all recursive problem can be solved by iterationAny iterative problem is solved recursively
It uses StackIt does not use Stack
Small Line Of CodeLarge Line of Code
Slow PerformanceFast Performance
Recursion is always applied to functions.Iteration is applied to iteration statements or “loops”.




Recursion

  • A procedure that contains a procedure call to itself or a procedure call to a second procedure which eventually causes the first procedure to be called is known as a recursive procedure.
  • There are two important conditions that must be satisfied by any recursive procedure
  1. Each time a procedure calls itself it must be nearer in some sense to a solution
  2. There must be a decision criterion for stopping the process or computation

C program for GCD using recursion

#include<stdio.h>
int Find_GCD(int, int);

void main()
{
int n1, n2, gcd; scanf(“%d %d”,&n1, &n2); gcd = Find_GCD(n1, &n2);
printf(“GCD of %d and %d is %d”, n1, n2, gcd);
}

int Find_GCD(int m, int n)
{
int gcdVal; if(n>m)
{
gcdVal = Find_GCD(n,m);
}
else if(n==0)
{
 

}
else
{

}
 
gcdVal = m;



gcdVal = Find_GCD(n, m%n);
 
return(gcdVal);
}



Iteration Factorial Program

#include <stdio.h>
 
int main() {
    int i, n = 5, fac = 1;
 
    for(i = 1; i <= n; ++i)
        fac = fac * i;
 
    printf("Factorial for 5 is %d", fac);
 
    return 0;
}

Related Differences:



LEAVE A REPLY

Please enter your comment!
Please enter your name here