Java program to check if all the digits of a number are in increasing order :
In this tutorial, we will learn how to check if all the digits of a number are in increasing/ascending order or not using Java. For example, for the number 12345
, all the digits are in increasing order. But for the number 54321
, they are not in increasing order.
The user will first enter one number and our program will start scanning its digits from right to left. It will compare the rightmost element to the element left to it.
For example, for the number 1234
, we will first compare 4
with 3
. Since 3
is smaller than 4
, the rightmost digit will be removed and it will be changed to 123
. Again, compare the digit 3
to 2
. If we found any rightmost digit smaller than the second rightmost digit, it will exit from the loop and print one failure message.
Let me show you different ways to write it in Java:
Method 1: Java Program with a while
loop:
The following program uses a while
loop to check if all digits of a user-input number are in increasing order:
import java.util.Scanner;
class FirstExample {
public static void main(String args[]) {
// 1
int num;
boolean isIncreasing = true;
// 2
try (Scanner scanner = new Scanner(System.in)) {
// 3
System.out.println("Enter a number : ");
num = scanner.nextInt();
// 4
int currentDigit = num % 10;
num = num / 10;
// 5
while (num > 0) {
// 6
if (currentDigit <= num % 10) {
isIncreasing = false;
break;
}
currentDigit = num % 10;
num = num / 10;
}
// 7
if (!isIncreasing) {
System.out.println("Digits are not in increasing order.");
} else {
System.out.println("Digits are in increasing order.");
}
}
}
}
Download it on GitHub
Explanation :
-
Initialize one integer variable
num
to store the user input number and one boolean variableisIncreasing
to indicate if the digits in the number are in increasing or decreasing order. The value ofisIncreasing
will beTrue
if the digits are in increasing order,False
otherwise. -
Create one
Scanner
object to read the user input number. -
Ask the user to enter a number. Read it and store it in the
num
variable. -
Create one integer variable
currentDigit
. It will hold the rightmost digit of the number. Convertnum
tonum/10
, i.e. remove the last digit of the number. -
Run one
while
loop until the value ofnum
is greater than0
. -
If any right digit is smaller than or equal to the left digit, assign
false
toisIncreasing
and break from the loop. Else, assign the rightmost digit tocurrentDigit
and update the value ofnumber
tonumber / 10
. -
Check the value of
isIncreasing
and print one message to the user. IfisIncreasing
is true, digits are in increasing order, else they are in decreasing order.
Sample Output :
Enter a number :
123456
Digits are in increasing order.
Enter a number :
1234586
Digits are not in increasing order.
Enter a number :
1368
Digits are in increasing order.
Method 2: Java Program with a separate method:
We can use a separate method to check if all digits of a number are in increasing or decreasing order. This method will take the number as its parameter and return one boolean value. It will return true if the numbers are in increasing order, else it will return false.
import java.util.Scanner;
class SecondExample {
public static boolean isDigitsAscending(int num) {
int currentDigit = num % 10;
num = num / 10;
while (num > 0) {
if (currentDigit <= num % 10) {
return false;
}
currentDigit = num % 10;
num = num / 10;
}
return true;
}
public static void main(String args[]) {
int num;
try (Scanner scanner = new Scanner(System.in)) {
System.out.println("Enter a number : ");
num = scanner.nextInt();
if (isDigitsAscending(num)) {
System.out.println("Digits are in increasing order.");
} else {
System.out.println("Digits are not in increasing order.");
}
}
}
}
Download it on GitHub
- We created a new method
isDigitsAscending
to check if the digits of a number are in ascending or descending order. - It uses a
while
loop similar to the previous example. It returns one boolean value based on the digits of the number in increasing or decreasing order.
It prints the same output.
Method 3: By converting the number to a String
:
With this approach, we need to convert the number to a String
. Once it is converted to a String
, we can iterate over the characters to compare each character with the next character. The following program shows how it works:
import java.util.Scanner;
class ThirdExample {
public static boolean isDigitsAscending(int num) {
String numString = String.valueOf(num);
for (int i = 0; i < numString.length() - 1; i++) {
if (numString.charAt(i) > numString.charAt(i + 1)) return false;
}
return true;
}
public static void main(String args[]) {
int num;
try (Scanner scanner = new Scanner(System.in)) {
System.out.println("Enter a number : ");
num = scanner.nextInt();
if (isDigitsAscending(num)) {
System.out.println("Digits are in increasing order.");
} else {
System.out.println("Digits are not in increasing order.");
}
}
}
}
Download it on GitHub
- The integer
num
is converted to a stringnumString
. - It iterates over the characters of the string and compares each character with the next character. It returns
false
if any two digits are not in increasing order. Else it returnstrue
at the end of the method.
If you run this program, it will print similar results.
Similar tutorials :
- Java program to check if a Matrix is Sparse Matrix or Dense Matrix
- Java Program to check if a number is Neon or not
- Java program to check if a number is perfect or not
- Java program to check if a number is Pronic or Heteromecic
- Java program to check if a matrix is upper triangular matrix or not
- Java program to check if a number is a buzz number or not