Recursion vs Iteration – Difference between Recursion and Iteration

Recursion vs Iteration. What’s the difference between recursion and iteration. Recursion and Iteration both are two different programming approaches. In some cases recursion is best suited and in some other cases iterative way of programming is good.

In programming, repeated set of instructions can be handled either by using recursive or iterative approach in your code. So which approach you choose and why. Let’s talk about recursion vs iteration.

Multiply two numbers without using multiplication operator

Recursion vs Iteration

i) In recursion, function call itself until the base condition is reached.

On other hand iteration means repetition of process until the condition fails. For example –  when you use loop (for,while etc.) in your programs.

ii) Iterative approach involves four steps, initialization , condition, execution and  updation.

In recursive function, only base condition (terminate condition) is specified.

iii) Recursion keeps your code short and simple Whereas iterative approach makes your code longer.

iv) Recursion is slower than iteration due to overhead of maintaining stack whereas iteration is faster.

v) Recursion takes more memory than iteration due to overhead of maintaining stack  .

vi) If recursion is not terminated (or base condition is not specified) than it creates stack overflow (where your system runs out of memory).

vii) Any recursive problem can be solved iteratively . But you can’t  solve all problems using recursion.

MCQ on Recursion

Subscribe Our Tutorials

Example of Programs using Recursion and Iteration Method

Let’s solved some program using both recursive and iterative approach.

Reverse a linked list using recursion

Print fibonacci series using recursion

i) Find Factorial of a Number using Recursion

Factorial Program using Iterative Method

ii) Sum of N Natural Numbers using Recursion

Sum of N Natural Numbers using Iteration

Important Points for Recursive and Iterative Approach

1. Recursion keeps your code short and clean as compared to iteration.

2. On other hand Recursion uses more memory than iteration due to excessive use of call stack.

There are some problems which can be efficiently solved using recursion such as

1. Travesals (Tree, Graph search).

2. Sorting algorithms (Merge Sort, Quicksort)