Java Program to Print Natural Numbers in Reverse

Write a Java Program to Print Natural Numbers in Reverse using For Loop, and While Loop with example.

Java Program to Print Natural Numbers in Reverse Example 1

This Java program allows the user to enter any integer value (the maximum limit value). Next, this Java program returns all the natural numbers from maximum limit value to 1 using For Loop.

// Java Program to Print Natural Numbers in Reverse 
import java.util.Scanner;

public class ReverseNaturalNum1 {
	private static Scanner sc;
	public static void main(String[] args) 
	{
		int number, i;
		sc = new Scanner(System.in);
		
		System.out.print(" Please Enter the Maximum integer Value : ");
		number = sc.nextInt();	
		
		for(i = number; i >= 1; i--)
		{
			System.out.print(i +"\t"); 
		}	
	}
}
Java Program to Print Natural Numbers in Reverse 1

First, we used the For loop to iterate from the maximum value (Here, number = 5) to 1.

User entered value: number  = 5

For Loop First Iteration: for(i = 5; i >= 1; i–)
Condition is True. So, i Value printed (i.e., 5)

Second Iteration: for(i = 4; 4 >= 1; 4–)
Condition is True. So, it prints 4

Third Iteration: for(i = 3; 3 >= 1; 3–)
Condition is True. It prints 3

Fourth Iteration: for(i = 2; 2 >= 1; 2–)
Condition is True. It prints 2

Fifth Iteration: for(i = 1; 1 >= 1; 1–)
Condition is True. 1 printed

Sixth Iteration: for(i = 0; 0 >= 1; 0–)
Condition (0 >= 1) is False. So, the Java compiler exits from the For Loop.

Java Program for Natural Numbers in Reverse Example 2

This Java program to display reverse natural numbers is the same as the above example, but we are using the While Loop.

// Java Program to Print Natural Numbers in Reverse 
import java.util.Scanner;

public class ReverseNaturalNum2 {
	private static Scanner sc;
	public static void main(String[] args) 
	{
		int number, i;
		sc = new Scanner(System.in);
		
		System.out.print(" Please Enter the Maximum integer Value : ");
		number = sc.nextInt();	
		
		i = number;
		
		while(i >= 1)
		{
			System.out.print(i +" "); 
			i--;
		}	
	}
}

Java natural numbers in reverse order using a while loop output

 Please Enter the Maximum integer Value : 20
20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 

Java Program to Print Natural Numbers in Reverse using Method

This program to print natural numbers in reverse order is the same as the first example. But we separated the logic and placed it in the separate Java method.

// Java Program to Print Natural Numbers in Reverse 
import java.util.Scanner;

public class ReverseNaturalNum3 {
	private static Scanner sc;
	public static void main(String[] args) 
	{
		int number;
		sc = new Scanner(System.in);
		
		System.out.print(" Please Enter the Maximum integer Value : ");
		number = sc.nextInt();	
		
		ReverseNaturalNumbers(number);	
	}
	
	public static void ReverseNaturalNumbers(int num)
	{
		int i;
		
		for(i = num; i >= 1; i--)
		{
			System.out.print(i +" "); 
		}	
	}
}
Please Enter the Maximum integer Value : 30
30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 

Java Program for Natural Numbers in Reverse between a Range

This Java program allows the user to enter Minimum and maximum value. Next, the program prints natural numbers between Maximum value and Minimum value.

// Java Program to Print Natural Numbers in Reverse 
import java.util.Scanner;

public class ReverseNaturalNum4 {
	private static Scanner sc;
	public static void main(String[] args) 
	{
		int minimum, maximum;
		sc = new Scanner(System.in);
		
		System.out.print(" Please Enter the Minimum value : ");
		minimum = sc.nextInt();			
		
		System.out.print(" Please Enter the Maximum value : ");
		maximum = sc.nextInt();	
		
		ReverseNaturalNumbers(minimum, maximum);
	}
	public static void ReverseNaturalNumbers(int min, int max)
	{
		int i;
		
		for(i = max; i >= min; i--)
		{
			System.out.print(i +" "); 
		}	
	}
}
Please Enter the Minimum value : 5
 Please Enter the Maximum value : 65
65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5