Every programmer knows What is a recursive function or Recursion in Java but when it comes to applying Recursion to a problem, many fumbles. As per my experience, Recursion is a tricky programming concept, some people get it very quickly, but for some programmers, it takes ages to understand Recursion and develop the ability to apply it.

I know, for some people recursion is very tough to master, I was one of them but when you keep solving problems like this, from easy to difficult, you will slowly understand how recursive solution works and come up with a recursion solution for any new coding problem.

Recursion will also help you to solve dynamic programming-based coding problems, which is very important from a coding interview perspective. Recursions are also one of the most powerful techniques to solve linked lists and binary tree-based problems because both linked lists and binary trees are recursive data structures.

In this Java tutorial, we will see some Recursion based example programs written in the Java programming language. You can also use this as a Java programming exercise.

**best Recursion courses for programmers**to learn how to identity recursive problems and how to break them down to solve using recursion.

##
__What is Recursion?__

Just a recap of what we have discussed about Recursion in Java in our earlier article, Recursion means calling himself. **A function or method is said to be Recursion if it calls itself**. To write a recursion function, the first thing anyone needs is to find the base case.

The base case is a particular case that can be solved without calling a recursive function. The base case is the endpoint for recursive function, it’s the point from where the stack starts winding up. Without any base case, the recursive function will result in StackOverFlowError.

So whenever you need to write a recursive function, first write a base case. Now let’s see a couple of examples of base cases in a recursive function.

##
__How to find if a condition can be solved using Recursion?__

Finding whether a programming problem can be solved using Recursion is a skill, not everybody sees issues in terms of Recursion. The best thing is to break the problem into a smaller set and see if the smaller problem is the same as the original problem or not like in order to calculate a factorial of 5, does it helps to calculate a factorial of 4? This may be a guide to see if Recursion can be used or not.

Similarly, String questions, Array-based problems, linked list algorithms, binary tree algorithms, and dynamic programming-based coding problems are good candidates for Recursion in Java.

Since a smaller linked list is a linked list, a smaller tree is a tree itself, problems like reversing the linked list, traversing the tree, etc. can be solved using Recursion in Java.

Steps to solve a problem using Recursion

Steps to solve a problem using Recursion

Once you have identified that a coding problem can be solved using Recursion, You are just two steps away from writing a recursive function.

1. Find the base case

2. Finding how to call the method and what to do with the return value.

As discussed above, finding a base case for any recursive solution is the first step towards writing a recursive function in Java or any other programming language. This is usually the simplest way to solve the problem without using recursion.

For example, when you calculate factorial, the base case is factorial(0) which is 1, you mean you know the answer so you can directly return it and from there onwards recursion will unroll and calculate factorial for the given number.

##
__15 Recursion Programming Exercises for Java Programmers__

As I said the best way to learn Recursion in Java is to do examples, here are some of the programming exercises which can be solved using Recursion in Java. These recursion exercises are not too difficult and fun to solve, so try to solve them yourself before looking at answers and solutions.

1. Write a program to calculate factorial using recursion in Java? (solution)

2. Write a program to Print Fibonacci Series in Java using Recursion? (solution)

3. Write a program to reverse String in Java using Recursion? (solution)

4. Write a countDown(int number) method in Java using Recursion which prints countdown till zero to console, like count(3) should print 3 2 1 0

hint:

```
public static void countDown(int number){
if (number == 0) {
System.out.println(number);
} else {
System.out.println(number);
countDown(number - 1);
}
}
```

5. Write a Java program to reverse Linked List using Recursion? (solution)

6. Program to reverse a number using Recursion in Java? (solution)

7. Write a Java program to print digitsToWords(int number) for example digitToWords(321) should print three two ones? (solution)

8. Write a Java program to calculate the power of a number like power(int number, int power) like power(2, 3) should return 8? (solution)

9. Write a program to calculate the sum of arithmetic series from 1 to N? (solution)

10. Write a program to calculate Greatest Common Division GCD using Euclid’s algorithm (solution)

That’s all on these** 15 Java Recursion examples and exercises**. Once you are comfortable with these easy recursive exercises you can move on to more complex recursive exercises like the famous Tower of Hanoi problem and several other dynamic programming-based problem which requires recursive solutions.

I highly recommend you to try solving this problem by yourself, without taking help from the internet because that’s the only way to learn recursion, your mind needs to be trained to understand the recursive solutions. Only see the solution if you can’t solve it on your own.

**Important Resources**for Coding and Programing Interviews:

Thanks a lot for reading this article so far. If you like these recursion-based coding problems and found this article useful in learning Recursion and recursion solutions, then please share them with your friends and colleagues. If you have any questions or feedback, then please drop a note.

**P. S. –**If you need a resource to understand recursion patterns, I highly recommend going through these

**best Recursion courses for programmers**to learn how to identity recursive problems and how to break them down to solve using recursion.

## More Stories like this

## So you want to expose your JAX-RS services over HTTP/2 – Java Code Geeks

## Difference between wait() and join() methods in Java [Answered]

## Top 12 Java Thread, Concurrency, and Multithreading Interview Questions with Answers