Google News
logo
Java Program to Bouncy Number
A bouncy number is a number whose digits are neither increasing nor decreasing in order.

For example, 538 is a bouncy number since its digits are not in increasing order (the digit 3 is less than 5) and are not in decreasing order (the digit 8 is greater than 3).

To determine if a number is bouncy, we can check each digit of the number and compare it to the previous digit. If the current digit is greater than the previous digit and we have already found a previous digit that was smaller than the previous digit, then the number is bouncy.

Similarly, if the current digit is less than the previous digit and we have already found a previous digit that was greater than the previous digit, then the number is also bouncy.

Here is a Java program that checks if a given number is bouncy :
Program :
import java.util.Scanner;

public class BouncyNumber {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int n = sc.nextInt();
        if (isBouncy(n)) {
            System.out.println(n + " is a bouncy number.");
        } else {
            System.out.println(n + " is not a bouncy number.");
        }
    }

    public static boolean isBouncy(int n) {
        boolean increasing = false;
        boolean decreasing = false;
        int prevDigit = n % 10;
        n /= 10;
        while (n > 0) {
            int currDigit = n % 10;
            if (currDigit > prevDigit && decreasing) {
                return true;
            } else if (currDigit < prevDigit && increasing) {
                return true;
            }
            if (currDigit < prevDigit) {
                decreasing = true;
            } else if (currDigit > prevDigit) {
                increasing = true;
            }
            prevDigit = currDigit;
            n /= 10;
        }
        return false;
    }

}
Output :
Enter a number: 538
538 is a bouncy number.
Enter a number: 631
631 is not a bouncy number.
In this program, we first prompt the user to enter a number. Then, we call the isBouncy() function to check if the entered number is bouncy or not. The isBouncy() function takes an integer argument and returns a boolean value indicating whether the number is bouncy or not.

The isBouncy() function uses two boolean variables increasing and decreasing to keep track of whether the digits are increasing or decreasing. We then iterate through each digit of the number by repeatedly dividing it by 10 and taking the remainder. We compare the current digit to the previous digit and set the increasing or decreasing flag accordingly.

If we have already found a previous digit that was smaller than the previous digit and we find a current digit that is greater than the previous digit, or if we have already found a previous digit that was greater than the previous digit and we find a current digit that is less than the previous digit, then the number is bouncy and we return true. If we make it through the entire loop without finding a bouncy pattern, then the number is not bouncy and we return false.