Check a given number is jumbled or not

In this tutorial we see what is jumbled numbers .  A number is said to be Jumbled if for every digit, its neighbours digit differs by max 1. It means number 123 , 454565 , 876543454567678 etc. are jumbled numbers  here . We will write a program for given number jumbled numbers in java or not .

Program to check jumbled numbers In Java

This java program check given numbers are jumbled numbers or not using while loop.


public class JumbledNumber {
	

   public static boolean isJumbledNumber(int num) 
    { 
        // Single digit number 
        if (num / 10 == 0) 
            return true; 
      
        // Checking every digit 
        // through a loop 
        while (num != 0)  
        { 
      
            // All digits were checked 
            if (num / 10 == 0) 
                return true; 
      
            // Digit at index i 
            int digit1 = num % 10; 
      
            // Digit at index i-1 
            int digit2 = (num / 10) % 10; 
      
            // If difference is  
            // greater than 1 
            if (Math.abs(digit2 - digit1) > 1) 
                return false; 
      
            num = num / 10; 
          } 
       return true; 
    } 
      
    // Driver code 
    public static void main (String[]args) 
    { 
        int num = 12345; 
      
        if (isJumbledNumber(num)) 
            System.out.println("Given number "+num+" is JumbledNumber"); 
        else
            System.out.println("Given number "+num+" is not JumbledNumber"); 
      
        
        
        num = 1541; 
      
        if (isJumbledNumber(num)) 
            System.out.println("Given number "+num+" is JumbledNumber");      
        else
            System.out.println("Given number "+num+" is not JumbledNumber");
              
    } 
}

Output :

Given number 12345 is JumbledNumber
Given number 1541 is not JumbledNumber

We have seen in this article what is jumbled numbers , example of jumbled numbers and how to check given numbers are jumbled numbers or not using java.

Leave a Reply

Your email address will not be published. Required fields are marked *

9 + 1 =