Difference between Recursion and Iteration Last Updated : 28 May, 2025 Suggest changes Share Like Article Like Report 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 <script> // JavaScript program to find factorial of given number // ----- Recursion ----- // method to find factorial of given number function factorialUsingRecursion(n) { if (n == 0) return 1; // recursion call return n * factorialUsingRecursion(n - 1); } // ----- Iteration ----- // Method to find the factorial of a given number function factorialUsingIteration(n) { var res = 1, i; // using iteration for (i = 2; i <= n; i++) res *= i; return res; } // Driver method var num = 5; document.write("Factorial of " + num + " using Recursion is: " + factorialUsingRecursion(5)+"<br>"); document.write("Factorial of " + num + " using Iteration is: " + factorialUsingIteration(5)); // This code is contributed by shivanisinghss2110 </script> PHP <?php // PHP program to find factorial of given number // ----- Recursion ----- // method to find factorial of given number function factorialUsingRecursion($n) { if ($n == 0) return 1; // recursion call return $n * factorialUsingRecursion($n - 1); } // ----- Iteration ----- // Method to find the factorial of a given number function factorialUsingIteration($n) { $res = 1; // using iteration for ($i = 2; $i <= $n; $i++) $res *= $i; return $res; } // Driver method $num = 5; print("Factorial of ".$num." using Recursion is: ". factorialUsingRecursion(5)."\n"); print("Factorial of ".$num." using Iteration is: ". factorialUsingIteration(5)."\n"); // This code is contributed by mits ?> OutputFactorial of 5 using Recursion is: 120 Factorial of 5 using Iteration is: 120Difference between Iteration and RecursionThe following table lists the major differences between iteration and recursion:PropertyRecursionIterationDefinitionFunction calls itself.A set of instructions repeatedly executed.ApplicationCertain 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.TerminationThrough base case, where there will be no function call.When the termination condition for the iterator ceases to be satisfied.UsageUsed in academics to teach foundations and logic. Also works as a foundation for Dynamic Programming and Divide and Conquer algorithms.Preferred in general.Code SizeCan be smaller code size for inherently recursive problems.Can be larger for naturally recursive problems.Time ComplexityIn 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 ComplexityIn 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 lowerOverheadPossesses overhead of repeated function calls.No overhead as there are no function calls in iteration. Advertise with us Next Article Types of Recursions D dkp1903 Follow Similar Reads Introduction to Recursion The process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution 14 min read What is Recursion? Recursion is defined as a process which calls itself directly or indirectly and the corresponding function is called a recursive function.Example 1 : Sum of Natural Numbers Let us consider a problem to find the sum of natural numbers, there are several ways of doing that but the simplest approach is 8 min read Difference between Recursion and Iteration 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 6 min read Types of Recursions What is Recursion? The process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. Using recursive algorithm, certain problems can be solved quite easily. Examples of such problems are Towers of Hanoi (TOH), Inord 15+ min read Finite and Infinite Recursion with examples The process in which a function calls itself directly or indirectly is called Recursion and the corresponding function is called a Recursive function. Using Recursion, certain problems can be solved quite easily. Examples of such problems are Towers of Hanoi (TOH), Inorder/Preorder/Postorder Tree Tr 6 min read What is Tail Recursion Tail recursion is defined as a recursive function in which the recursive call is the last statement that is executed by the function. So basically nothing is left to execute after the recursion call.For example the following function print() is tail recursive.C++// An example of tail recursive funct 7 min read What is Implicit recursion? What is Recursion? Recursion is a programming approach where a function repeats an action by calling itself, either directly or indirectly. This enables the function to continue performing the action until a particular condition is satisfied, such as when a particular value is reached or another con 5 min read Why is Tail Recursion optimization faster than normal Recursion? What is tail recursion? Tail recursion is defined as a recursive function in which the recursive call is the last statement that is executed by the function. So basically nothing is left to execute after the recursion call. What is non-tail recursion? Non-tail or head recursion is defined as a recur 4 min read Recursive Functions A Recursive function can be defined as a routine that calls itself directly or indirectly. In other words, a recursive function is a function that solves a problem by solving smaller instances of the same problem. This technique is commonly used in programming to solve problems that can be broken do 4 min read Difference Between Recursion and Induction Recursion and induction are fundamental ideas in computer science and mathematics that might be regularly used to solve problems regarding repetitive structures. Recursion is a programming technique in which a function calls itself to solve the problem, whilst induction is a mathematical proof techn 4 min read Article Tags : Technical Scripter DSA Technical Scripter 2018 Algorithms-Recursion Like