Exploring Recursive Functions in Java: A Guide to Problem Solving

Kushagra Ojha
4 min readJun 7, 2023

Introduction

Recursion is a powerful concept in computer programming that allows us to solve complex problems by breaking them down into smaller, more manageable subproblems. In this article, we will explore recursive functions in Java and demonstrate their application through a series of examples. Specifically, we will dive into recursive functions that find the product of numbers in a list, capitalize words in an array, find the highest element in an array, and add a 10% tax to each salary. By the end of this article, you will have a solid understanding of how to leverage recursion to solve a variety of problems.

Calculating the Product of Numbers in a List

The first problem we will tackle is finding the product of all numbers in a list. We will define a recursive function getProductRecursive that multiplies each element with the product of the remaining elements until it reaches the end of the list. We will provide the implementation of this function and explain how it works step by step. We will also include a complete code example to illustrate its usage.

public class Main {
public static int getProductRecursive(int[] arr, int index) {
if (index < 0 || index >= arr.length) {
return 1;
} else {
return arr[index] * getProductRecursive(arr, index + 1);
}
}

public static void main(String[] args) {
int[] numbers = {2, 3, 4, 5};
int product = getProductRecursive(numbers, 0);
System.out.println("Product: " + product);
}
}

Capitalizing Words in an Array:

Next, we will explore a recursive function to capitalize all words in an array. We will define the function capitalizeWordsRecursive that converts each word to uppercase using the toUpperCase method and calls itself recursively to process the remaining words in the array. We will break down the logic of this function and demonstrate its usage with a code snippet and output.

import java.util.Arrays;

public class Main {
public static String[] capitalizeWordsRecursive(String[] words, int index) {
if (index < 0 || index >= words.length) {
return new String[0];
} else {
String word = words[index];
String capitalizedWord = word.toUpperCase();
String[] remainingWords = capitalizeWordsRecursive(words, index + 1);
String[] result = new String[remainingWords.length + 1];
result[0] = capitalizedWord;
System.arraycopy(remainingWords, 0, result, 1, remainingWords.length);
return result;
}
}

public static void main(String[] args) {
String[] words = {"foo", "bar", "world"};
String[] capitalizedWords = capitalizeWordsRecursive(words, 0);
System.out.println("Capitalized words: " + Arrays.toString(capitalizedWords));
}
}

Finding the Highest Element in an Array

In this section, we will tackle the problem of finding the highest element in an array using recursion. We will define the recursive function findHighestElementRecursive that compares each element with the current highest value and updates it if a higher element is found. We will explain the base case and recursive step of this function and provide a code example to illustrate its usage.

public class Main {
public static int findHighestElementRecursive(int[] arr, int index, int highest) {
if (index < 0 || index >= arr.length) {
return highest;
} else {
int currentElement = arr[index];
if (currentElement > highest) {
highest = currentElement;
}
return findHighestElementRecursive(arr, index + 1, highest);
}
}

public static void main(String[] args) {
int[] numbers = {5, 8, 2, 10, 3};
int highestElement = findHighestElementRecursive(numbers, 0, Integer.MIN_VALUE);
System.out.println("Highest element: " + highestElement);
}
}

Adding Tax to Salaries

Lastly, we will address the task of adding a 10% tax to each salary in an array. We will define the recursive function addTaxRecursive that calculates the new salary by adding the tax and calls itself recursively to process the remaining salaries. We will discuss the implementation details of this function and provide a complete code example to demonstrate its functionality.

import java.util.Arrays;

public class Main {
public static double[] addTaxRecursive(double[] salaries, int index) {
if (index < 0 || index >= salaries.length) {
return new double[0];
} else {
double salary = salaries[index];
double newSalary = salary + (0.1 * salary);
double[] remainingSalaries = addTaxRecursive(salaries, index + 1);
double[] result = new double[remainingSalaries.length + 1];
result[0] = newSalary;
System.arraycopy(remainingSalaries, 0, result, 1, remainingSalaries.length);
return result;
}
}

public static void main(String[] args) {
double[] salaries = {1000.0, 2000.0, 3000.0};
double[] newSalaries = addTaxRecursive(salaries, 0);
System.out.println("New salaries: " + Arrays.toString(newSalaries));
}
}

Conclusion

Recursion is a powerful technique that allows us to solve complex problems by breaking them down into smaller, more manageable subproblems. In this article, we explored recursive functions in Java through a series of examples. We learned how to calculate the product of numbers in a list, capitalize words in an array, find the highest element in an array, and add tax to salaries using recursive functions. By leveraging recursion, we can write elegant and efficient code to solve a wide range of problems. So, the next time you encounter a problem that can be solved recursively, embrace the power of recursion and dive into the world of elegant problem-solving.

Thanks for Reading ❤❤

--

--

Kushagra Ojha

Exploit Developer | Malware Analyst | Security Researcher