Difference between Recursion and Iteration (original) (raw)
Last Updated : 28 May, 2025
A program is called recursive when an entity calls itself. A program is called iterative when there is a loop (or repetition).
**Example: Program to find the factorial of a number
C++ `
// C++ program to find factorial of given number #include<bits/stdc++.h> using namespace std;
// ----- Recursion ----- // method to find factorial of given number int factorialUsingRecursion(int n) { if (n == 0) return 1;
// recursion call
return n * factorialUsingRecursion(n - 1);
}
// ----- Iteration ----- // Method to find the factorial of a given number int factorialUsingIteration(int n) { int res = 1, i;
// using iteration
for (i = 2; i <= n; i++)
res *= i;
return res;
}
// Driver method int main() { int num = 5; cout << "Factorial of " << num << " using Recursion is: " << factorialUsingRecursion(5) << endl;
cout << "Factorial of " << num <<
" using Iteration is: " <<
factorialUsingIteration(5);
return 0;
}
// This code is contributed by mits
C
// C program to find factorial of given number #include <stdio.h>
// ----- Recursion ----- // method to find factorial of given number int factorialUsingRecursion(int n) { if (n == 0) return 1;
// recursion call
return n * factorialUsingRecursion(n - 1);
}
// ----- Iteration ----- // Method to find the factorial of a given number int factorialUsingIteration(int n) { int res = 1, i;
// using iteration
for (i = 2; i <= n; i++)
res *= i;
return res;
}
// Driver method int main() { int num = 5; printf("Factorial of %d using Recursion is: %d\n", num, factorialUsingRecursion(5));
printf("Factorial of %d using Iteration is: %d", num,
factorialUsingIteration(5));
return 0;
}
// This code is contributed by mits
Java
// Java program to find factorial of given number class GFG {
// ----- Recursion -----
// method to find factorial of given number
static int factorialUsingRecursion(int n)
{
if (n == 0)
return 1;
// recursion call
return n * factorialUsingRecursion(n - 1);
}
// ----- Iteration -----
// Method to find the factorial of a given number
static int factorialUsingIteration(int n)
{
int res = 1, i;
// using iteration
for (i = 2; i <= n; i++)
res *= i;
return res;
}
// Driver method
public static void main(String[] args)
{
int num = 5;
System.out.println("Factorial of " + num
+ " using Recursion is: "
+ factorialUsingRecursion(5));
System.out.println("Factorial of " + num
+ " using Iteration is: "
+ factorialUsingIteration(5));
}
}
Python
Python3 program to find factorial of given number
----- Recursion -----
method to find factorial of given number
def factorialUsingRecursion(n): if (n == 0): return 1;
# recursion call
return n * factorialUsingRecursion(n - 1);
----- Iteration -----
Method to find the factorial of a given number
def factorialUsingIteration(n): res = 1;
# using iteration
for i in range(2, n + 1):
res *= i;
return res;
Driver method
num = 5; print("Factorial of",num,"using Recursion is:", factorialUsingRecursion(5));
print("Factorial of",num,"using Iteration is:", factorialUsingIteration(5));
This code is contributed by mits
C#
// C# program to find factorial of // given number using System;
class GFG {
// ----- Recursion -----
// method to find factorial of
// given number
static int factorialUsingRecursion(int n)
{
if (n == 0)
return 1;
// recursion call
return n * factorialUsingRecursion(n - 1);
}
// ----- Iteration -----
// Method to find the factorial of
// a given number
static int factorialUsingIteration(int n)
{
int res = 1, i;
// using iteration
for (i = 2; i <= n; i++)
res *= i;
return res;
}
// Driver Code
public static void Main(String[] args)
{
int num = 5;
Console.WriteLine("Factorial of " + num +
" using Recursion is: " +
factorialUsingRecursion(5));
Console.WriteLine("Factorial of " + num +
" using Iteration is: " +
factorialUsingIteration(5));
}
}
// This code has been contributed by Rajput-Ji
JavaScript
PHP
`
Output
Factorial of 5 using Recursion is: 120 Factorial of 5 using Iteration is: 120
Difference between Iteration and Recursion
The following table lists the major differences between iteration and recursion:
Property | Recursion | Iteration |
---|---|---|
**Definition | Function calls itself. | A set of instructions repeatedly executed. |
**Application | Certain problems can be solved quite easily using recursion like Towers of Hanoi (TOH), Inorder/Preorder/Postorder Tree Traversals, DFS of Graph, etc. | In general iterative solutions are preferred over recursive solutions as there is no extra overhead required for recursion. |
**Termination | Through base case, where there will be no function call. | When the termination condition for the iterator ceases to be satisfied. |
**Usage | Used in academics to teach foundations and logic. Also works as a foundation for Dynamic Programming and Divide and Conquer algorithms. | Preferred in general. |
**Code Size | Can be smaller code size for inherently recursive problems. | Can be larger for naturally recursive problems. |
Time Complexity | In general time complexity is higher or same. The time complexity especially become higher for the problems that can be optimized using dynamic programming | Lower or same. |
**Space Complexity | In general, the auxiliary space is either higher. Can be same in same cases where we need an explicit stack to simulate recursion like tree traversals, merge sort, etc. | Generally lower |
**Overhead | Possesses overhead of repeated function calls. | No overhead as there are no function calls in iteration. |