Increasing, Decreasing & Bouncy Numbers Program in Java | ISC Computer Science 2023 Paper 2

An integer whose digits are arranged in ascending order from left to right is called an increasing number. At any position, the current digit is always greater than or equal to the previous digit. For example, 1233, 13689, 112334566, etc.

An integer whose digits are arranged in descending order from left to right is called a decreasing number. Here, the current digit is always less than or equal to the previous digit. For example, 321, 88531, 8755321, etc.

A positive integer where the digits are neither in increasing nor decreasing order is called a bouncy number. In other words, if the digits of the number are unsorted, then it is bouncy. For example, 123742, 101, 43682, etc.

Design a program to accept a number (containing only digits 0 to 9) from the user. For an invalid input, display an appropriate message. Check whether the number is an increasing, decreasing or a bouncy number and display with an appropriate message in the format specified below:

Example 1
INPUT: Enter a number: 122344
OUTPUT: 122344 IS AN INCREASING NUMBER

Example 2
INPUT: Enter a number: 887552
OUTPUT: 887552 IS A DECREASING NUMBER

Example 3
INPUT: Enter a number: 185349
OUTPUT: 185349 IS A BOUNCY NUMBER

Example 4
INPUT: Enter a number: 98#57-649
OUTPUT: INVALID INPUT

import java.util.Scanner;
class Bouncy{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        System.out.print("Enter a number: ");
        String num = in.nextLine();
        if(!valid(num)){
            System.out.println("INVALID INPUT");
            return;
        }
        boolean ascending = false;
        boolean descending = false;
        for(int i = 1; i < num.length(); i++){
            char previous = num.charAt(i - 1);
            char current = num.charAt(i);
            if(previous < current)
                ascending = true;
            else if(previous > current)
                descending = true;
        }
        if(ascending && descending)
            System.out.println(num + " IS A BOUNCY NUMBER");
        else if(ascending)
            System.out.println(num + " IS AN INCREASING NUMBER");
        else
            System.out.println(num + " IS A DECREASING NUMBER");
    }
    public static boolean valid(String s){
        for(int i = 0; i < s.length(); i++){
            char ch = s.charAt(i);
            if(!Character.isDigit(ch))
                return false;
        }
        return true;
    }
}