Examples of Iterative and Recursive Algorithms in Java

Examples of Iterative and Recursive Algorithms in Java

An algorithm is a procedure or formula for solving a problem. If you need to repeat some steps in the procedure you can opt Iterative algorithm or Recursive algorithm. Recursion and iteration are just two different code structures with the same end result. The difference between them is that recursion is simply a method call in which the method being called is the same as the one making the call while iteration is when a loop is repeatedly executed until a certain condition is met.

Examples of Iterative Algorithm

Java BinarySearch

Recursion

Recursion is a process in which a method(function) calls itself directly(simple recursion) or indirectly(through other functions(complex or indirect recursion)). For example function A calls function B, and function B calls function A. The number of nested calls to a function(or procedure) is called recursion depth. A recursive function allows you to write clean code, without explicit repetition of parts of the code and the use of loops. The Recursive Algorithm uses a selection structure and makes code smaller.

Factorial

Iterative Algorithm

Recursive Algorithm

Recursive algorithm

How to Sum the numbers from 1 to N

Iterative Algorithm

Recursive Algorithm

Arithmetic Progression

https://en.wikipedia.org/wiki/Arithmetic_progression

 

Count number of occurrences in a given array

Iterative Algorithm

Recursive Algorithm

Recursive Function to Find Maximum Element in an Array

Sum of Array Elements Using Recursion

Java Program to Check Prime Numbers