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
mainreadsnum, createsBouncyNumber ob = new BouncyNumber(num)and callsob.display().- The constructor stores
numinnand sets bothincanddectotrue. display()prints an invalid message ifn <= 0; otherwise it callsscanDigits()and then printsnwith the text fromtype().scanDigits()takesright = n % 10andx = n / 10. Whilex > 0:left = x % 10- if
left > right, setinc = false - if
left < right, setdec = false right = left,x = x / 10
type()returns "Increasing and Decreasing" if both flags are true, "an increasing number" if onlyinc, "a decreasing number" if onlydec, 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
| Variable | Data Type | Purpose |
|---|---|---|
n | int | Data member: the number to be classified |
inc | boolean | Data member: stays true until a digit falls from left to right |
dec | boolean | Data member: stays true until a digit rises from left to right |
num (in constructor) | int | Parameter: the number passed to the constructor |
x | int | Digits of n not yet compared, reduced each pass of the loop |
right | int | The digit to the right of the pair being compared |
left | int | The digit to the left of the pair, x % 10 |
sc | Scanner | Reads the number in main |
num (in main) | int | The number typed by the user |
ob | BouncyNumber | Object created in main |
args | String[] | Command-line arguments of main (not used) |
Sample input and output
Sample 1
Enter a number: 1234
1234 is an increasing numberSample 2
Enter a number: 9631
9631 is a decreasing numberSample 3
Enter a number: 1342
1342 is a bouncy numberSample 4
Enter a number: 7
7 is Increasing and DecreasingSample 5
Enter a number: 0
Invalid input: enter a positive numberDry run
Input: 1342, so n = 1342, inc = true, dec = true after the constructor.
x | right | left | left > right | left < right | inc | dec |
|---|---|---|---|---|---|---|
| 134 | 2 | 4 | true | false | false | true |
| 13 | 4 | 3 | false | true | false | false |
| 1 | 3 | 1 | false | true | false | false |
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
- Range "Write a program to display all bouncy numbers between two given limits m and n."
- Counting "Write a program to count the bouncy numbers below a given limit."