Finite and Infinite Recursion with Examples in Java5 May 2025 | 2 min read Recursion is the process by which a function calls itself, either directly or indirectly, and the associated function is known as a recursive function. Recursion makes it easy to solve some difficulties. Towers of Hanoi (TOH), Inorder/Preorder/Postorder Tree Traversals, DFS, and other issues are examples of the problems that can be solved using the Recursion. Types of Recursions: Two different kinds of recursion can be distinguished based on when they terminate:
Finite Recursion:A finite number of recursive calls is required for the recursion to end, which is known as finite recursion. When a base condition is satisfied, a recursion comes to an end. It makes sure the function ends when its task is completed. Implementation:FileName: FiniteRecursionExample.java Output: 5 4 3 2 1 Complexity Analysis: The time complexity is O(N), and the space complexity of the above code is O(N) Infinite Recursion:When the recursion continues after a finite number of recursive calls, it is known as infinite recursion. The recursion continues indefinitely since the base condition is never satisfied. The program will then keep making recursive calls until a StackOverflowError occurs as an output. Implementation:FileName: InFiniteRecursionExample.java Output: 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 Complexity Analysis: The time complexity is non-finite since the recursion will never end, and the space complexity of the above code is non-finite. Next TopicAdvantages of Python over Java |
Nth Term of Geometric Progression in Java
Three numbers are given. The first number is the first term of the geometric progression. The second number is the common ratio of the geometric progression, and the third number is the nth term that has to be computed. Example 1: Input int a1 = 5, // first term int...
4 min read
Tetranacci Number in Java
In this section, we will learn what is a Tetranacci number and also create Java programs to check if the given number is a Tetranacci number or not. The Tetranacci number program is frequently asked in Java coding interviews and academics. Tetranacci Number Tetranacci numbers are similar to...
3 min read
NoClassDefFoundError in Java
Just like ClassNotFoundException, NoClassDefFoundError occurs at runtime. We get this error when the class is not available in the program at runtime. It is an unchecked exception which a program throws when the requested class is not present at runtime. In this case, the class was...
3 min read
RuleBasedCollator compare() method in Java with Example
The java.text.RuleBasedCollator class has a compare() function. When two objects are compared for strength, the RuleBasedCollator class is used to compare the results. Based on the comparison, the class returns a value that is either positive or negative. Syntax: public int compare(Object obj1, Object obj2) Parameter:...
3 min read
Various Operations on Queue Using Linked List in Java
In computer programming, queues are a basic data structure used to organise collections of elements in a specific order. The java.util.Queue interface and the java.util.LinkedList class are just two of the built-in classes and interfaces provided by Java for working with queues. In this article, we'll...
4 min read
Tribonacci Number Java
In this section, we will learn what is Tribonacci number and also create Java programs to that calculates the Tribonacci number. The Tribonacci number program is frequently asked in Java coding interviews and academics. Tribonacci Number Tribonacci numbers are the same as Fibonacci numbers. We can get the...
3 min read
Javac Command Not Found
Javac command is usually used to check whether the Java is installed in our system or not. When the Java is not installed in our system and we try to run Javac command, we get Javac command not found or Java is not Recognized. We can...
2 min read
Java Web Development
Web development is known as website development or web application development. The web development creates, maintains, and updates web development applications using a browser. This web development requires web designing, backend programming, and database management. The development process requires software technology. Web development creates web applications using...
6 min read
Online Java IDE
An effective and dependable Integrated Development Environment (IDE) is a key tool in the world of programming. It improves productivity, streamlines development, and provides programmers with a feature-rich environment. s have become a practical and accessible option for developers with the emergence of cloud computing and...
3 min read
Longest Subarray With All Even or Odd Elements in Java
An array inputArr[] is given to us that contains non-negative numbers. Our task is to find the length of the longest subarray such that all of the elements of that subarray are either even or odd. Example: 1 Input: int arr[] = {5, 5, 3, 7, 9, 7, 0,...
9 min read
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India