Bouncy Number in Java

An increasing number is a non-negative integer in which the digits from left to right are arranged in such a way that no digit on the left is greater than the digit to its right. Example: 588999.

On the other hand, a decreasing number is a non-negative integer in which the digits from left to right are arranged in such a way that no digit on the left is smaller than the digit to its right. Example: 95442.

And finally, a bouncy number is one in which is a non-negative integer, and it is neither an increasing number, nor a decreasing number. Example: 264458.

Write a program in Java to input an integer, and check whether it is an increasing number, or a decreasing number or a bouncy number, and display a suitable message accordingly.

import java.io.*;
class Bouncy{
    public static void main(String args[])throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("N = ");
        int n = Integer.parseInt(br.readLine());
        if(n < 0){
            System.out.println("Not an increasing number.");
            System.out.println("Not a decreasing number.");
            System.out.println("Not a bouncy number.");
            return;
        }
        if(isIncreasing(n))
            System.out.println(n + " is an increasing number.");
        if(isDecreasing(n))
            System.out.println(n + " is a decreasing number.");
        if(!isIncreasing(n) && !isDecreasing(n))
            System.out.println(n + " is a bouncy number.");
    }
    public static boolean isIncreasing(int num){
        int last = num % 10;
        boolean status = true;
        while(num != 0){
            int digit = num % 10;
            if(digit > last){
                status = false;
                break;
            }
            last = digit;
            num /= 10;
        }
        return status;
    }
    public static boolean isDecreasing(int num){
        int last = num % 10;
        boolean status = true;
        while(num != 0){
            int digit = num % 10;
            if(digit < last){
                status = false;
                break;
            }
            last = digit;
            num /= 10;
        }
        return status;
    }
}

Leave a Reply