Order check java – Java Program to Check if All the Digits of a Number are in Increasing Order

Order check java: In the previous article, we have seen Java Program to Add Zeros to Start of a Number

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

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

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

For example:

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

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

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

Method-1: Java Program to Check if All the Digits of a Number are in Increasing 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 greater 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();
        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 ascending 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 ascending order.");
        else
            System.out.println(num+" digits are not in ascending order");
    }
}
Output:

Enter a number to check - 456
456 digits are in ascending order

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

Approach:

  • Ask the user to enter the number and store it.
  • Then call a method to check order of digits by passing that number as parameter.
  • Use a while loop to iterate through all the digits.
  • If a current digit is greater 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 ascending 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 ascending order.");
        else
            System.out.println(num+" digits are not in ascending order");
    }
}
Output:

Case-1
Enter a number to check - 2479
2479 digits are in ascending order.

Case-2
Enter a number to check - 82671
2479 digits are not in ascending 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.

Related Java Programs: