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

ISC Practicals and Theory - Bouncy, Increasing and Decreasing Number (OOP)

Updated 20 Oct 2026

A number is increasing if its digits never fall from left to right, decreasing if they never rise, and bouncy if they do both. This version uses a class BouncyNumber with a parameterised constructor, two boolean data members and one method for each job.

Definition

Read the digits left to right. Equal neighbouring digits are allowed.

  • Increasing: each digit is greater than or equal to the digit on its left (1234, 1122).
  • Decreasing: each digit is less than or equal to the digit on its left (9631, 5500).
  • Bouncy: neither of the two (1342).
  • Increasing and Decreasing: all digits equal, such as 111 or any single digit; the number is both increasing and decreasing, and the program prints exactly these words for it.

Zero and negative numbers are treated as invalid input.

Algorithm

  1. main reads num, creates BouncyNumber ob = new BouncyNumber(num) and calls ob.display().
  2. The constructor stores num in n and sets both inc and dec to true.
  3. display() prints an invalid message if n <= 0; otherwise it calls scanDigits() and then prints n with the text from type().
  4. scanDigits() takes right = n % 10 and x = n / 10. While x > 0:
    • left = x % 10
    • if left > right, set inc = false
    • if left < right, set dec = false
    • right = left, x = x / 10
  5. type() returns "Increasing and Decreasing" if both flags are true, "an increasing number" if only inc, "a decreasing number" if only dec, otherwise "a bouncy number".

A single scan sets both flags, so the digits are read only once.

Program

scanDigits() updates the flags, type() turns the flags into words and display() validates and prints.

import java.util.*;

/**
 * Class BouncyNumber (OOP version) classifies a number as increasing,
 * decreasing, both increasing and decreasing, or bouncy. The object is
 * created with the number; one method scans the digits and another
 * reports the type.
 */
public class BouncyNumber
{
    int n;              // the number to be classified
    boolean inc;        // true while no digit has fallen from left to right
    boolean dec;        // true while no digit has risen from left to right

    /**
     * Parameterised constructor: stores the number and assumes both
     * properties are true until a digit pair proves otherwise.
     * @param num the number to be classified
     */
    BouncyNumber(int num)
    {
        n = num;
        inc = true;
        dec = true;
    }

    /** Compares every pair of neighbouring digits and updates inc and dec. */
    void scanDigits()
    {
        int x = n / 10;             // remaining digits
        int right = n % 10;         // digit to the right of the pair
        while (x > 0)
        {
            int left = x % 10;      // digit to the left of the pair
            if (left > right)
                inc = false;        // a fall: not increasing
            if (left < right)
                dec = false;        // a rise: not decreasing
            right = left;           // move one place to the left
            x = x / 10;
        }
    }

    /**
     * Gives the type of the number after the digits have been scanned.
     * @return the words that complete the sentence "n is ..."
     */
    String type()
    {
        if (inc && dec)
            return "Increasing and Decreasing";
        else if (inc)
            return "an increasing number";
        else if (dec)
            return "a decreasing number";
        else
            return "a bouncy number";
    }

    /** Validates n, scans it and displays the type. */
    void display()
    {
        if (n <= 0)
            System.out.println("Invalid input: enter a positive number");
        else
        {
            scanDigits();
            System.out.println(n + " is " + type());
        }
    }

    /** main method: reads the number, creates the object and calls display(). */
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int num = sc.nextInt();
        BouncyNumber ob = new BouncyNumber(num);
        ob.display();
    }
}

Variable description table

VariableData TypePurpose
nintData member: the number to be classified
incbooleanData member: stays true until a digit falls from left to right
decbooleanData member: stays true until a digit rises from left to right
num (in constructor)intParameter: the number passed to the constructor
xintDigits of n not yet compared, reduced each pass of the loop
rightintThe digit to the right of the pair being compared
leftintThe digit to the left of the pair, x % 10
scScannerReads the number in main
num (in main)intThe number typed by the user
obBouncyNumberObject created in main
argsString[]Command-line arguments of main (not used)

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: 7
7 is Increasing and Decreasing

Sample 5

Enter a number: 0
Invalid input: enter a positive number

One value per line

Dry run

Input: 1342, so n = 1342, inc = true, dec = true after the constructor.

xrightleftleft > rightleft < rightincdec
13424truefalsefalsetrue
1343falsetruefalsefalse
131falsetruefalsefalse

Then x becomes 0 and the loop ends. With both flags false, type() returns "a bouncy number" 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. Counting "Write a program to count the bouncy numbers below a given limit."