Base case and recursive case

Base case and recursive case

In Java Programming, recursion, the base case and recursive case are two important structural elements of any recursive method. The base case and recursive case concepts in a recursion function method help Java users when they need to control or fix the start and end Behavior of the recursion.

Base case and recursive case

So, let’s better understand recursive and base case in Java programming.

Recursive Base Case / Recursion Termination Condition.

In Java, the base case is a user-defined condition in a recursive function that indicates when the recursion function should terminate. It defines a small solution to the user-defined problem in an easy method, which can be solved directly if needed, without any further recursive calls. Without a base case in recursion, the recursive function would continue calling itself infinitely, causing a stack overflow. In detail, the recursion base case terminates the recursion function method before an infinite number of times, and defines the termination procedure for the recursion function method.

Features of the Base Case in recursion.

  • Stops the recursion – This is the element in recursion where the recursion method stops.
  • Simplest form – This represents the simplest possible input or condition for a recursive problem in recursion, which can be solved directly if needed.
  • Stops infinite recursion – Here, without a base case in recursion, the user-defined recursive function would continue calling itself forever.

Factorial example from a base case in recursion.

In Java programming, programmers define a recursive base case. The recursive factorial function can be defined as follows:

n!=n×(n−1)×(n−2)×⋯×1n!=n×(n−1)×(n−2)×⋯×1

0!=10!=1 (base case defined here)

Here, the base case is defined for the factorial function. When n=0, the factorial function should return 1.

Example of Base Case Factorial in Java.

public class Main

{

// here is the recursive method used to calculate the factorial value.

public static int fact(int n) {

if (n == 0) {

return 1; // here is the base case: define factorial of 0 as 1.

} else {

return n * fact(n – 1); // Here we define the recursive case

}

}

public static void main(String[] args) {

int output = fact(7); // output is 7! = 5040

System.out.println(“\nThe factorial of 7 is – ” + output);

}

}

Explanation of the Base Case Factorial.

In this program, when the base case is defined as n=0, the fact function method directly returns the value 1. This process stops the recursive calls. In this, when the recursive case is defined as n > 0, the method calls itself with the value n-1, and it continues running until it reaches the base case.

Recursive Case/Recursive Call in Java.

In a recursive function, the recursive case is the function element or structure where it calls itself with a modified function method argument. This process or step converts a large problem size into a smaller one, thereby getting closer to the base case in subsequent recursive calls.

Features of the recursive case in Java.

  • Self-call – This recursive case method calls itself with new variable parameters, thereby making the problem size smaller in recursive programming.
  • Smaller problem – Here, a complex problem becomes smaller or easier with each recursive procedure call.
  • Leading to the base case – Ultimately, the recursive calls should reach the base case.

Java recursive case factorial example.

Here, this line defines the recursive case function in the factorial program example.

return n * factorial(n – 1);

In this recursive function, each time the function calls itself, it decrements the value n by 1, gradually leading to the base case of n=0.

Base Case vs. Recursive Case in Java.

  • Base Case – In recursion, the base case directly displays the result for simple input and terminates the infinite recursion process.
  • Recursive Case – In recursion, a recursive function is called with a modifiable variable parameter (usually a small or simple form of the problem), which ultimately leads to the base case.

Fibonacci Sequence Example in Base and Recursive Case in Java.

The Fibonacci sequence in Java programs is a series of numbers in which each number is the sum of the two preceding numbers.

F(0)=0F(0)=0

F(1)=1F(1)=1

F(n)=F(n−1)+F(n−2)F(n)=F(n−1)+F(n−2) for n ≥ 2 n ≥ 2

Recursive Fibonacci Method in Java Programming.

public class main

{

// here we define Recursive method to calculate the nth number of Fibonacci number sequence

public static int fibonacci_num(int n) {

if (n == 0) {

return 0; // here Base case – define F(0) = 0

} else if (n == 1) {

return 1; // here recursive Base case – define F(1) = 1

} else {

return fibonacci_num(n – 1) + fibonacci_num(n – 2); //here Recursive case method define

}

}

public static void main(String[] args) {

int output = fibonacci_num(9); // Output is F(9) = 34

System.out.println(“\n the Fibonacci of 9 number is – ” + output);

}

}

Explanation of the Recursive Fibonacci Method.

  • In the Fibonacci program, the base case is when n=0, where it returns 0. When n=1, it returns 1. This is an easy Fibonacci method defined here.
  • In the recursive case, when n≥2, the function calls itself twice, once with n−1 and once with n−2. In this process, the recursive calls break the problem until they reach the targeted base case value.

Leave a Reply