Check if a Number is Bouncy in Java



What is Bouncy Number?

In mathematical terms, a Bouncy number is a positive integer whose digits are neither in increasing nor in decreasing order. For example: 101, 102, 103, 104, 105, 106, 107, 108, 109, and 120 are the bouncy numbers, which digits does not follow any specific order.

The bouncy number will always be unsorted, and there are no bouncy numbers between the range of 1 to 100 because numbers less than 100 can have only two digits that will be either increasing or decreasing order.

Input & Output Scenarios

The following input and output scenarios will implement the mathematical calculation to check for the bouncy number:

Scenario 1

Suppose the given number is 101:

Input: 1 > 0 and 0 < 1 (unsorted order)
Output: Yes

Since the digits (1, 0, 1) of the input number 101 are neither in decreasing nor in increasing order, it is considered as a bouncy number.

Scenario 2

Suppose we have a input number 245:

Input: 2 < 4 but 4 < 5 (increasing order)
Output: No

Since the digits (2, 4, 5) of the given number 245 are in increasing order, it will not be considered a bouncy number.

Explanation

Here is the solution explanation that will help you write a program for checking bouncy numbers:

  • First, check if the number is greater than 100; if not number is not a bouncy number.
  • Check the order digits of the given number; if the sorted order print number is not a bouncy number.
  • If the order of digits is unsorted, print the number as a bouncy number.

Example 1

The following example checks whether the number 6549 is a bouncy number. First, we will check the order of digits in the given number and accordingly will compare each digit to check the bouncy number:

public class checkBouncy{
   public static void main(String args[]){   
   
      int num = 6549;
      System.out.println("The given number is: " + num);
      // Check if the number is less than 100
      if(num < 100){
         System.out.println( num + " is a not a bouncy number (num < 100)");
      }
      
      // Boolean values to check if the digits are sorted
      boolean increasing = true, decreasing = true;
      
      int temp = num;
      int prev = num % 10;
      
      while(temp != 0){
         // Checks if it follows increasing order or not
         if(temp % 10 > prev){
            increasing = false;
            break;
         }
         prev = temp % 10;
         temp /= 10;
      }
      
      // reset the value
      temp = num;
      prev = num % 10;
      
      while(temp != 0){
         // Checks if it follows decreasing order or not
         if(temp % 10 < prev){
            decreasing = false;
            break;
         }
         prev = temp % 10;
         temp /= 10;
      }
      
      if(!increasing && !decreasing){
         System.out.println("Yes! " + num + " is a bouncy number");
      }
      else{
         System.out.println("No! " + num + " is not a bouncy number");
      }
   }
}

Output

The above program produces the following output:

The given number is: 6549
Yes! 6549 is a bouncy number

Example 2

This is another example of checking for a bouncy number. We define three different methods to check the decreasing and increasing order of digits in the given number 146. The third checkBouncy() method will compare each digit and check whether the number is a bouncy number:

public class checkBouncy{

   //define a method to check number increasing order
   static boolean isIncreasing(int num){

      // Converts the integer to a string
      String s = Integer.toString(num);
      char dig;
      boolean result = true;

      for(int i = 0; i < s.length()-1; i++){

        //using charAt() method to access each string element
         dig = s.charAt(i);
         if(dig > s.charAt(i+1)){
            result = false;
            break;
         }
      }
      return result;
   }

   // define a method to check number decreasing order
   static boolean isDecreasing(int num){

    // Converts the integer to a string
      String s = Integer.toString(num);
      char dig;
      boolean result = true;
      for(int i = 0; i < s.length()-1; i++){

        //using charAt() method to access each string element
         dig = s.charAt(i);
         if(dig > s.charAt(i+1)){
            result = false;
            break;
         }
      }
      return result;
   }

   //define a method to check bouncy number
   static boolean bouncyNum(int num){

      // Checks if the number is less than 100
      if(num  < 100){

         System.out.println("The number is not a bouncy number");
      }

      if(!isIncreasing(num) && !isDecreasing(num)){
         return true;
      }
      else{
         return false;
      }
   }
   public static void main(String args[]){ 

      int num = 146;
      System.out.println("The given number is: " + num);

      // Calls the method by passing the num value to it
      boolean res = bouncyNum(num);

      if(res){
         System.out.println("Yes! " + num + " is a bouncy number");
      }
      else{
         System.out.println("No! " + num + " is not a bouncy number");
      }
   }
}

Output

Following is the output of the above program:

The given number is: 146
No! 146 is not a bouncy number

Example 3

If a number is less than 100, it "will not be considered" a bouncy number, as bouncy numbers must be "greater than 100":

public class checkBouncy{
   public static void checkBouncyNumber(int num){
              // Check if the number is less than 100
      if(num < 100){
         System.out.println("Number less than 100 can't be a bouncy number.");
      }
      
      else{
      
         // Boolean values to check if the digits are sorted
         boolean increasing = true, decreasing = true;
         
         int temp = num;
         int prev = num % 10;
         
         while(temp != 0){
            // Checks if it follows increasing order or not
            if(temp % 10 > prev){
               increasing = false;
               break;
            }
            prev = temp % 10;
            temp /= 10;
         }
         
         // reset the value
         temp = num;
         prev = num % 10;
         
         while(temp != 0){
            // Checks if it follows decreasing order or not
            if(temp % 10 < prev){
               decreasing = false;
               break;
            }
            prev = temp % 10;
            temp /= 10;
         }
         
         if(!increasing && !decreasing){
            System.out.println("Yes! " + num + " is a bouncy number");
         }
         else{
            System.out.println("No! " + num + " is not a bouncy number");
         }
      }
   }
   public static void main(String args[]){   
   
      int num = 89;
      System.out.println("The given number is: " + num);
      
      //calling the checkBouncyNumber() method to check bouncy number
      checkBouncyNumber(num);
   }
}

Output

Below is the output of the above program:

The given number is: 89
Number less than 100 can't be a bouncy number.
Updated on: 2025-05-29T18:17:10+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements