Prashnikaप्रश्निका
Code
About
‹ All Computer Science notes

ISC Practicals and Theory - Bouncy, Increasing and Decreasing Number

Updated 21 Oct 2026

Numbers can be sorted by how their digits move from left to right. If the digits never fall, the number is increasing; if they never rise, it is decreasing; if they do both, it is bouncy. This note gives the method, an ISC-style Java program written in the main method alone, sample runs and a dry run.

Definition

Read the digits from left to right. Equal neighbouring digits are allowed in an increasing or a decreasing number.

  • Increasing number: each digit is greater than or equal to the digit on its left. Example: 1234, 1122.
  • Decreasing number: each digit is less than or equal to the digit on its left. Example: 9631, 5500.
  • Bouncy number: neither increasing nor decreasing, so the digits both rise and fall. Example: 1342. Reading left to right, 1 to 3 rises, 3 to 4 rises and 4 to 2 falls, so the digits do both.

A number whose digits are all equal (for example 111, or any single digit such as 5) is both increasing and decreasing, and the program prints Increasing and Decreasing for it. Zero and negative numbers are treated as invalid input.

Algorithm

  1. Accept the number n. If n <= 0, print an invalid-input message and stop.
  2. Set inc = true and dec = true. Set right = n % 10 (the last digit) and x = n / 10 (the remaining digits).
  3. While x > 0:
    • take the next digit to the left, left = x % 10;
    • if left > right, the digits fall when read from left to right, so set inc = false;
    • if left < right, the digits rise, so set dec = false;
    • set right = left and remove the digit with x = x / 10.
  4. If both inc and dec are true, all the digits are equal and the number is both increasing and decreasing. If only inc is true it is increasing, if only dec is true it is decreasing, and if both are false it is bouncy.

Program

One loop in main compares each pair of neighbouring digits and clears inc or dec; the if ladder at the end prints the type.

import java.util.*;
/**
 * Program Bouncy classifies a number as increasing, decreasing, both or bouncy.
 * Increasing: no digit is smaller than the digit on its left (e.g. 1234, 1122).
 * Decreasing: no digit is larger than the digit on its left (e.g. 9631, 5500).
 * A number with all digits equal is both increasing and decreasing.
 * Bouncy: neither increasing nor decreasing (e.g. 1342).
 */
public class Bouncy
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int n = sc.nextInt();                   // the number to be classified
        if (n <= 0)
        {
            System.out.println("Invalid input: enter a positive number");
        }
        else
        {
            boolean inc = true;                 // stays true if the digits never fall from left to right
            boolean dec = true;                 // stays true if the digits never rise from left to right
            int x = n / 10;                     // remaining digits
            int right = n % 10;                 // digit to the right of the one being tested
            while (x > 0)
            {
                int left = x % 10;              // next digit to the left
                if (left > right)               // a fall when read left to right
                    inc = false;
                if (left < right)               // a rise when read left to right
                    dec = false;
                right = left;
                x = x / 10;
            }
            if (inc && dec)
                System.out.println(n + " is Increasing and Decreasing");
            else if (inc)
                System.out.println(n + " is an increasing number");
            else if (dec)
                System.out.println(n + " is a decreasing number");
            else
                System.out.println(n + " is a bouncy number");
        }
    }
}

Variable description table

VariableData TypePurpose
argsString[]Command line arguments of main; not used
scScannerReads the number typed by the user
nintThe number to be classified
incbooleanStays true while the digits never fall from left to right
decbooleanStays true while the digits never rise from left to right
xintThe digits of n still to be examined; the last digit is removed in each pass
rightintThe digit to the right of the one being tested
leftintThe next digit to the left, taken in the current pass

Sample input and output

Sample 1

Enter a number: 1234
1234 is an increasing number

Sample 2

Enter a number: 9631
9631 is a decreasing number

Sample 3

Enter a number: 1342
1342 is a bouncy number

Sample 4

Enter a number: 111
111 is Increasing and Decreasing

Sample 5

Enter a number: -5
Invalid input: enter a positive number

One value per line

Dry run

Input: 1342. Digits from the right are compared in pairs (left, right). inc and dec both start as true.

PassxrightleftTestincdec
1134244 > 2 truefalsetrue
213433 < 4 truefalsefalse
31311 < 3 truefalsefalse

So inc = false and dec = false, and the program prints 1342 is a bouncy number.

Variations

  1. Range "Write a program to display all bouncy numbers between two given limits m and n."
  2. Increasing or decreasing only "Write a program to check whether a number is an increasing number."
  3. Counting "Write a program to count the bouncy numbers below a given limit."