Java Program to Count Backward of a Number using a do-while loop

In this tutorial, we will write a program to count the number backward in java. The program uses a scanner class to take the input from the user and with a do-while loop, it counts backward and displays the result.

If you do not know the working process of do-while loop then click the link below.


Java Program to Count Backward of a Number

 //count backward of a number using do-while loop in java

 import java.util.Scanner;

 public class CountBackNumber
 {
   public static void main(String args[])
   {
     int n = 5;
     Scanner scanner = new Scanner(System.in);

     System.out.println("Enter the number from where you want to back count: ");
     n = scanner.nextInt();

     do {
       System.out.print(n + " ");
       n--;  // Decrementing N by 1 in each iteration
     } while (n >= 0);
   }
 }

Output:

Enter the number from where you want to back count:
10
10 9 8 7 6 5 4 3 2 1 0