In this tutorial, you will learn how to write a Java program to print numbers from n to 1 by taking the value of n as input from the user.
For example, if the user enters the number 10, the program will print numbers in descending order, starting from 10 and ending at 1.
Output:
10 9 8 7 6 5 4 3 2 1
In Java, we can print numbers in descending order using different looping statements, such as for, while, and do-while. We can also use the decrement operator (--) to decrease the value of a loop variable by 1 after each iteration.
Let’s understand each approach with complete Java programs and examples.
Java Program to Print Numbers from n to 1 Using for Loop
The for loop is one of the simplest and most commonly used approaches to print numbers from n to 1 in Java.
In this approach, we initialize a loop variable i with the user-entered value of n. The loop continues as long as i is greater than or equal to 1. After printing each number, i is decreased by 1 using the decrement operator (i--).
Let’s write a Java program to print numbers from n to 1 using the for loop.
Complete Java Program
import java.util.Scanner;
public class PrintNumbersDescending {
public static void main(String[] args) {
// Create a Scanner object to take user input
Scanner scanner = new Scanner(System.in);
// Prompt the user to enter the value of n
System.out.print("Enter the value of n: ");
int n = scanner.nextInt();
// Check whether the entered number is positive (greater than or equal to 1)
if (n >= 1) {
System.out.println("Numbers from " + n + " to 1 are:");
// Print numbers in descending order using a for loop
for (int i = n; i >= 1; i--) {
System.out.print(i + " ");
}
} else {
// Handle invalid input where n is less than 1
System.out.println("Please enter a positive integer.");
}
// Close the Scanner object to prevent resource leaks
scanner.close();
}
}Output:
Enter the value of n: 15 Numbers from 15 to 1 are: 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
Explanation of the Java Program
Let’s understand how this Java program works step by step.
- Import the Scanner class: First, we import the Scanner class from the java.util package to accept input from the keyboard.
- Create a Scanner object: We create a Scanner object of the Scanner class to read the user’s input.
- Read the value of n: The nextInt() method of the Scanner class reads an integer value entered by the user and stores it in the variable n.
- Validate the input: We use an if statement to check whether the entered number is greater than or equal to 1. If the number is less than 1, the program displays an appropriate message.
- Initialize the loop variable: Inside the for loop, we initialize the variable i with the value of n.
- Check the loop condition: The loop condition
i >= 1checks whether the current value of i is greater than or equal to 1. If the condition is true, the loop body executes. - Print the number: The statement
System.out.print(i + " ")prints the current value of i on the console. - Decrement the variable: The decrement operator (
i--) decreases the value of i by 1 after each iteration. - Terminate the loop: When the value of i becomes 0, the condition
i >= 1becomes false, and the loop terminates.
Thus, the above Java program prints all numbers from the user-entered value of n down to 1 in descending order.
Java Program to Print Numbers from n to 1 Using while Loop
You can also print numbers from n to 1 using a while loop in Java. A while loop repeatedly executes a block of statements as long as its specified condition evaluates to true.
In this approach, we initialize a control variable i with the value of n before the loop starts. The while loop checks whether i is greater than or equal to 1. If the condition is true, it prints the current value of i and decreases it by 1.
Let’s look at the complete Java program using a while loop.
Complete Java Program
import java.util.Scanner;
public class PrintNumbersWhileLoop {
public static void main(String[] args) {
// Create a Scanner object for user input
Scanner scanner = new Scanner(System.in);
// Read the value of n
System.out.print("Enter the value of n: ");
int n = scanner.nextInt();
// Check whether n is positive
if (n >= 1) {
System.out.println("Numbers from " + n + " to 1 are:");
// Initialize the loop variable
int i = n;
// Print numbers in descending order
while (i >= 1) {
System.out.print(i + " ");
// Decrease i by 1
i--;
}
} else {
System.out.println("Please enter a positive integer.");
}
// Close the Scanner object
scanner.close();
}
}Output:
Enter the value of n: 10 Numbers from 10 to 1 are: 10 9 8 7 6 5 4 3 2 1
Explanation of the Java Program
Let’s understand how this Java program works step by step.
- Import the Scanner class: We import the Scanner class from the java.util package to accept input from the user.
- Create a Scanner object: We create an object of the Scanner class to read the integer entered by the user.
- Read the value of n: The nextInt() method reads the input number and stores it in the variable n.
- Validate the input: We use an if statement to check whether the entered number is greater than or equal to 1.
- Initialize the loop variable: Before the while loop begins, we initialize the variable i with the value of n (
int i = n). - Check the condition: The condition
i >= 1checks whether the current value of i is greater than or equal to 1. If true, the loop body executes. - Print the number: The
System.out.print(i + " ")statement prints the current value of i followed by a space. - Decrement the variable: The
i--statement decreases the value of i by 1 after every iteration. - Terminate the loop: When i becomes 0, the condition evaluates to false, and the while loop terminates.
Thus, the program successfully prints all numbers from n down to 1 using a while loop.
Coding Challenge: Using Do-While
Your Task:
Write a complete Java program named PrintNumbersDoWhile that accomplishes the following:
- Prompts the user to enter an integer n.
- Validates that n is positive (n >= 1).
- If it is less than 1, display a polite error message.
- If valid, use a do-while loop to print all numbers from n down to 1 in descending order (separated by a space).
- Properly close the Scanner object.
Give it a try! You can use the structure of the while loop program you reviewed earlier as a reference for how to set up the input validation and structure.




