chevron_left
64 points
10 5 2

Whenever there is a task that needs to be done multiple time with different parameters but with the same body recursive methods can deliver a great help .

Mostly algorithms use this way to do smaller subtask that needs to be done repeatedly , this makes the code to more readable and easy to understand .

For loops can perform this tasks that are taken care of by recursive methods but in an old and unpleasant way so to make our code more cleaner and easy to read using recursive methods is advised.

First, consider the simplest instance of our problem. This is usually the case when the inputs to the problem are the simplest or smallest values possible and can generally be solved with a straightforward solution. In our problem, the simplest input possible is when the input num is less than 1. In this case, all we need to do is print a blank line. This case is called a base case, or stopping case—that is, a case containing no recursive calls. As we will see, when this case occurs recursion will end. Additionally, unless this simple case is made to work correctly, no other case will work correctly. Code for a method implementing the base case for our countdown problem is simple:

     public static void countDown(int num) { 
           if (num <= 0) {
                                   System.out.println();
                              } 
               }

This method handles the base case, but not the more interesting case when num is 1 or more. To solve this recursively we have to break the problem up into a smaller version of the same problem. This means rephrasing the problem in terms of a method call back to countDown. Let’s say that num equals 3. In other words, we have invoked countDown(3). We want to output “321”.

This is the same as outputting “3” followed by “21”. But outputting “21” is what countDown(2) should output! Our algorithm to solve this becomes

Output 3
countDown(2)—which is responsible for outputting 21
In turn, countDown(2) can repeat the same process:
Output 2
Call countDown(1)—which is responsible for outputting 1
Output 1
Call countDown(0)—which is responsible for outputting a newline
When countDown(0) is invoked, this executes the base case which prints a newline and ends the recursive method calls.
Output num
Call countDown(num-1)—which is responsible for counting from num-1 to 1

         public class RecursiveCountdown { 
                      public static void main(String[] args) {
                                    countDown(3); 
                      } 
         public static void countDown(int num) { 
                      if (num <= 0) { 
                                   System.out.println(); 
                      } 
                      else { 
                                   System.out.print(num); 
                                   countDown(num - 1); 
                      } 
         } 
 

More Posts

How to calculate a factorial of a given number in java using recursion muliemes - May 17
Sum of digits of a number using recursion in C sakshi - May 28
Find largest number in array using recursion java Mubashir Islam - Oct 26, 2020
Find minimum value in array recursion java Hasnain_khan - Oct 13, 2020
algorithm to find factorial of a number using recursion kesav.eee - Jun 23
Find the number of occurences of an object in collections - Java DivyaN - Sep 12
Find the percentage of lowercase, uppercase, digits, special characters in a string? Java DivyaN - Sep 1
get the first letter of a string in java DivyaN - Aug 16
name of the = operator in java? DivyaN - Aug 16
Get the length of a list in java usmanhashmi - Aug 6