Java Program to Check if All the Digits of a Number are in Decreasing Order

In the previous article, we have seen Java Program to Check if All the Digits of a Number are in Increasing Order

In this article we are going to check if all the digits of a number are in decreasing order in Java.

Java Program to Check if All the Digits of a Number are in Decreasing Order

We have to check if the digits in the number are in increasing order or not.

For example:

If a number is 8765 then digits in this number are 8, 7, 6, 5. So, all digits are in descending order.

Another number is 9683 then digits are 9, 6, 8 and 3. So all digits are not in descending order.

Let’s see different ways to check if all the digits of a number are in decreasing order.

Method-1: Java Program to Check if All the Digits of a Number are in Decreasing Order By Using User Input Value

Approach:

  • Ask the user to enter the number and store it.
  • Use a while loop to iterate through all the digits.
  • If a current digit is less than the next digit then it is not in decreasing order else it is.
  • Print the result.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        // Class to take input
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter a number to check - ");
        // Taking input from user
        int num = scan.nextInt();
        int temp = num;
        // Storing the first digit
        int dig = temp%10;
        temp=temp/10;
        // Flag to store the result
        boolean flag = true;

        // Loop that iterates through all digits
        while(temp>0)
        {
            // Checking if all digits are in descending order
            if(dig>=temp%10)
            {
                flag = false;
                break;
            }
            dig = temp%10;
            temp=temp/10;
        }
        // printing the result
        if(flag)
            System.out.println(num+" digits are in descending order.");
        else
            System.out.println(num+" digits are not in descending order");
    }
}
Output:

Enter a number to check - 6421
6421 digits are in descending order.

Method-2: Java Program to Check if All the Digits of a Number are in Decreasing Order By Using User Defined Method

Approach:

  • Ask the user to enter the number and store it.
  • Then call a method to check order order of digits by passing that number as parameter.
  • Use a while loop to iterate through all the digits.
  • If a current digit is less than the next digit then it is not in increasing order else it is.
  • Print the result.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        // Class to take input
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter a number to check - ");
        // Taking input from user
        int num = scan.nextInt();
        //calling checkOrder() method
        checkOrder(num);
    }
    
    public static void  checkOrder(int num)
    {
        int temp = num;
        // Storing the first digit
        int dig = temp%10;
        temp=temp/10;
        // Flag to store the result
        boolean flag = true;

        // Loop that iterates through all digits
        while(temp>0)
        {
            // Checking if all digits are in descending order
            if(dig>=temp%10)
            {
                flag = false;
                break;
            }
            dig = temp%10;
            temp=temp/10;
        }
        // printing the result
        if(flag)
            System.out.println(num+" digits are in descending order.");
        else
            System.out.println(num+" digits are not in descending order");
    }
}
Output:

Case-1
Enter a number to check - 8653
8653 digits are in descending order.

Case-2
Enter a number to check - 7854
7854 digits are not in descending order

Provided list of Simple Java Programs is specially designed for freshers and beginners to get familiarize with the concepts of Java programming language and become pro in coding.

Test yourself:

  1. How to check if numbers are in ascending order in java?
  2. How to check if numbers are in sequence python?
  3. How to check if numbers are in sequence c?
  4. How to check if numbers are in sequence javascript?
  5. How to check if an array is in descending order java?
  6. How to check if numbers are in sequence c++?
  7. How to check if numbers are in sequence javascript?
  8. How to check if numbers are in sequence java?
  9. Decreasing numbers code in java?

Related Java Programs: