In the previous article, we have discussed about Java Program to Check Disarium Number by Using Recursion
In this program we are going to see how to print N to 1 by using recursion in Java programming language.
Java Program to Print N to 1 by Using Recursion
Let’s understand the problem statement first with an example.
Assume the value of N = 5 Then the series from N to 1 = 5 4 3 2 1
Note- Take ‘N’ as any positive integer.
Now let’s see different ways to print N to 1 by using recursion.
Method-1: Java Program to Print N to 1 By Using Static Input and Recursion
Approach:
- Declare an integer variable say ‘
n’ and initialize the value. - Call a user defined method
series()method and pass ‘n’ as parameter. - Inside the user defined method we will check if the number is less than or equal to 0 then the value is returned to the method else it will print the number and then recursively call
series()method until it becomes 0 and the method execution terminates.
Program:
import java.util.*;
import java.io.*;
public class Main
{
public static void main(String[] args)
{
// declare and initialize an integer variable ‘n’
int n = 17;
System.out.println("The series from "+n+" to 1 is: ");
// calling series() method with n as parameter
series(n);
// exception handling if the number is less than or equal to 0
if(n<=0)
System.out.println("The series can't be defined, please enter a number greater than 0");
}
// Defining series() method
static void series(int n)
{
// here the series() method is terminated if the number is less than or equal to 0
if(n<=0)
return;
// print the number from N to 1
System.out.print(n + " ");
// calling series() method recursively until it reaches 0
series(n-1);
}
}
Output: The series from 17 to 1 is: 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
Method-2: Java Program to Print N to 1 By Using User Input and Recursion
Approach:
- Declare an integer variable say ‘
n’ and initialize the value. - Prompt the user to enter a number.
- Call a user defined method
series()method and pass ‘n’ as parameter. - Inside the user defined method we will check if the number is less than or equal to 0 then the value is returned to the method else it will print the number and then recursively call
series()method until it becomes 0 and the method execution terminates.
Program:
import java.util.*;
import java.io.*;
public class Main
{
public static void main(String[] args)
{
// create a scanner class
Scanner s = new Scanner(System.in);
System.out.println("Enter a number:");
//Prompt the user to enter a number
int n = s.nextInt();
System.out.println("The series from "+n+" to 1 is: ");
// calling series() method with n as parameter
series(n);
// exception handling if the number is less than or equal to 0
if(n<=0)
System.out.println("The series can't be defined, please enter a number greater than 0");
}
// Defining series() method
static void series(int n)
{
// here the series() method is terminated if the number is less than or equal to 0
if(n<=0)
return;
// print the number from N to 1
System.out.print(n + " ");
// calling series() method recursively until it reaches 0
series(n-1);
}
}
Output: Enter a number: -17 The series can't be defined, please enter a number greater than 0 The series from -17 to 1 is:
Practice Java programming from home without using any fancy software just by tapping on this Simple Java Programs for Beginners tutorial.
Related Java Programs: