Bouncy Number Program in Java with Examples

In this post I will be sharing what is Bouncy number, examples of Bouncy number, algorithm  and java program to check whether a given number is Bouncy number or not.

Before understanding what is Bouncy number, first we need to understand the terms increasing and decreasing numbers.

Read Also :  Java Program to Check Happy Number                   

Increasing Number

Moving from left to right, if no digit is exceeded by the digit to its left. Then the number is known as increasing number.

Below are examples of Increasing Numbers :

1233
23689
112334566

Read Also :  Check Given Number is Pronic or Not

Decreasing Number

Moving from left to right, if no digit is exceeded by the digit to its right. Then the number is known as decreasing number.

Below are examples of Decreasing Numbers :

321
88531
8755321

What is Bouncy Number

Bouncy number is any positive integer which is neither increasing nor decreasing number.

Below are examples of Bouncy numbers :

101
43682
123742

All numbers below 100 are not Bouncy number. 101 is the first bouncy number.

Java Program for Bouncy Number


import java.util.*;

public class JavaHungry {
    public static void main(String args[]) {
        System.out.println("Enter any number : ");
        Scanner scan = new Scanner(System.in);
        int inputNumber = scan.nextInt();
        if (isIncreasing(inputNumber) || isDecreasing(inputNumber) 
            || inputNumber < 101)
            System.out.println(inputNumber+" Not a Bouncy number");
        else
            System.out.println(inputNumber+" is a Bouncy number");
    }
    
    public static boolean isIncreasing(int inputNumber) {
        // Converting inputNumber to String
        String str = Integer.toString(inputNumber);
        char digit;
        boolean flag = true;
        for(int i=0;i < str.length()-1;i++) {
            digit = str.charAt(i);
            /*if any digit is greater 
            than next digit, Stop checking further*/
            if(digit > str.charAt(i+1)) {
                flag = false;
                break;
            }    
        }
        return flag;
    }
    
    public static boolean isDecreasing(int inputNumber) {
        // Converting inputNumber to String
        String str = Integer.toString(inputNumber);
        char digit;
        boolean flag = true;
        for(int i=0;i < str.length()-1;i++) {
            digit = str.charAt(i);
            /*if any digit is lesser 
            than next digit, Stop checking further*/
            if(digit < str.charAt(i+1)) {
                flag = false;
                break;
            }    
        }
        return flag;        
    }
}


Output :

Enter any number : 12345
12345 Not a Bouncy number 

Enter any number : 123453
123453 is a Bouncy number

Algorithm for Bouncy Number 

1. Convert inputNumber to String and store its value in variable str.

2. Initialize flag boolean variable to true. The final value of flag variable will decide whether the number is Bouncy number or not.

3. Using for loop
    a. Move from leftmost character to right.
    b. In isIncreasing() method, check if any digit is greater than next digit. If yes then set flag to false and come out of the for loop.
    c. In isDecreasing() method, check if any digit is lesser than next digit. If yes then set flag to false and come out of the for loop.

4. Return the value of flag variable.

That's all for the day, please mention in the comments if you know any other way to check whether a given number is Bouncy Number or not.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry