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
- Accept the number
n. Ifn <= 0, print an invalid-input message and stop. - Set
inc = trueanddec = true. Setright = n % 10(the last digit) andx = n / 10(the remaining digits). - 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 setinc = false; - if
left < right, the digits rise, so setdec = false; - set
right = leftand remove the digit withx = x / 10.
- take the next digit to the left,
- If both
incanddecare true, all the digits are equal and the number is both increasing and decreasing. If onlyincis true it is increasing, if onlydecis 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
| Variable | Data Type | Purpose |
|---|---|---|
args | String[] | Command line arguments of main; not used |
sc | Scanner | Reads the number typed by the user |
n | int | The number to be classified |
inc | boolean | Stays true while the digits never fall from left to right |
dec | boolean | Stays true while the digits never rise from left to right |
x | int | The digits of n still to be examined; the last digit is removed in each pass |
right | int | The digit to the right of the one being tested |
left | int | The next digit to the left, taken in the current pass |
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: 111
111 is Increasing and DecreasingSample 5
Enter a number: -5
Invalid input: enter a positive numberDry run
Input: 1342. Digits from the right are compared in pairs (left, right). inc and dec both start as true.
| Pass | x | right | left | Test | inc | dec |
|---|---|---|---|---|---|---|
| 1 | 134 | 2 | 4 | 4 > 2 true | false | true |
| 2 | 13 | 4 | 3 | 3 < 4 true | false | false |
| 3 | 1 | 3 | 1 | 1 < 3 true | false | false |
So inc = false and dec = false, 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."
- Increasing or decreasing only "Write a program to check whether a number is an increasing number."
- Counting "Write a program to count the bouncy numbers below a given limit."