## Recursion In Java

We will learn Java recursion with examples in this post so we can fulfill your educational needs.

## Java Recursion

Java recursion is the technique of making a function call itself.

The purpose of this technique is to simplify complex problems into simpler ones that can be solved easily.

In some cases, you may find recursion difficult to understand.

Experimentation is the key to discovering how it works.

## Java Recursion Example

Two numbers are easy to multiply together, but multiplying a range of numbers is more challenging.

When it comes to Java recursion, multiplying a range of numbers together is accomplished by breaking it down into the simple task of multiplying two numbers.

The following example shows how to multiply all 10 natural numbers starting from 1 which is also known as Factorial of 10:

#### Example:

public class Main { public static void main(String[] args) {// Factorial of 10 numbers =10 x 9 x 8 x 7 × 6 × 5 × 4 × 3 × 2 × 1int mrx = factorial_of_10_numbers(10); System.out.println(" Result = "+mrx); } public static int factorial_of_10_numbers(int ample) {if (ample > 1) { return ample * factorial_of_10_numbers(ample – 1); } else { return 1; } } // Output = Result= 3628800 }

### Example Explanation:

By using the factorial_of_ten_numbers() method, the parameter ample is multiplied by the product of all numbers smaller than ample, and the result is returned as mrx. When ample becomes 1, the function returns 1. During execution, the program performs the following steps:

``````10 * factorial_of_ten_numbers(9)
10 * ( 9 * factorial_of_ten_numbers(8) )
10 * ( 9 * ( 8 * factorial_of_ten_numbers(7) ) )
…
10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1
``````

When ample is 1, the function does not call itself, so the program stops there and returns the result.

The following example  shows how to sum all 15 natural numbers starting from 1:

#### Example:

public class Main { public static void main(String[] args) {// Sum of 20 numbers= 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 + 15int mrx = sum_of_15_numbers(15); System.out.println(" Result = "+mrx); } public static int sum_of_15_numbers(int ample) {if (ample > 1) { return ample + sum_of_15_numbers(ample – 1); } else { return 1; } } // Output = Result= 120 }

### Example Explanation:

By using the sum_of_15_numbers() method, the parameter ample is added to the sum of all numbers smaller than ample, and the result is returned as mrx. When ample becomes 1, the function returns 1. During execution, the program performs the following steps:

``````15 + sum_of_15_numbers(14)
15 + ( 14 + sum_of_15_numbers(13) )
15 + ( 14 + ( 13 + sum_of_15_numbers(12) ) )
…
15 + 14 + 13 + 12 + 11 + 10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1
``````

When ample is 1, the function does not call itself, so the program stops there and returns the result.

## Halting Condition

Infinite recursion is a problem associated with recursive functions, just as infinite looping is a problem associated with loops.

When a function calls itself infinitely, it is considered infinite recursion.

Recursive functions must have halting conditions where they can stop calling themselves. A halting condition in the previous example occurs when k becomes 1.

The concept can be better understood by seeing a variety of different examples. As shown in the example, the function multiplies a range of numbers between a start and an end. End must not be greater than start to halt this recursive function.

Using the example below, you can use recursion to product numbers between 3 and 8:

#### Example:

public class Main { public static void main(String[] args) { int var_num = product_numbers_between_3_to_8(3, 8); System.out.println("Result of Product of numbers between 3 and 8: "+var_num); } public static int product_numbers_between_3_to_8(int mrx_start, int ample_end) { if (ample_end > mrx_start) { return ample_end * product_numbers_between_3_to_8(mrx_start, ample_end – 1); } else { return ample_end; } } // Output: Result of Product of numbers between 3 to 8: 20160 }

Below is an example of how recursion can be used to sum numbers between 4 and 9:

#### Example:

public class Main { public static void main(String[] args) { int var_num = add_numbers_between_4_to_9(4, 9); System.out.println("Result of sum of numbers between 4 and 9: "+var_num); } public static int add_numbers_between_4_to_9(int mrx_start, int ample_end) { if (ample_end > mrx_start) { return ample_end + add_numbers_between_4_to_9(mrx_start, ample_end – 1); } else return ample_end; } // Output: Result of sum of numbers between 4 and 9: 39 }

• Recursive calls allocate new storage locations on the stack for variables.
• Each time a recursive call returns, the old variables and parameters are removed from the stack. As a result, recursion typically consumes more memory and is slower.
• In contrast, recursive solutions are much simpler to write, debug, and maintain.

The Java Recursion concept has now been explained to you, so you know how to take advantage of it.

+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0