Bouncy Number Program in Java
A bouncy number's digits are neither increasing nor decreasing, like 155349. Java program to print the bouncy numbers in a range, with logic and dry run.
A bouncy number is a positive integer whose digits are neither in increasing order nor in decreasing order.
- In an increasing number, each digit is greater than or equal to the digit before it, for example 1234 and 14779.
- In a decreasing number, each digit is less than or equal to the digit before it, for example 4321 and 9731.
- A number that is neither is bouncy, for example 155349: the digits go up from 1 to 5, down to 3 and up again to 9.
No number below 100 is bouncy, because one- and two-digit numbers are always increasing or decreasing. The first bouncy number is 101.
Question
Write a program to accept two numbers p and q and print all the bouncy numbers from p to q, along with how many there are. Use separate methods to check whether a number is increasing, decreasing and bouncy.
Logic
- Convert the number to a String so each digit can be read with
charAt(). isIncreasing()compares every digit with the one just before it. If any digit is smaller than the previous one, the number is not increasing.isDecreasing()does the same with the opposite comparison.- A number is bouncy if it is positive and is neither increasing nor decreasing.
Program
import java.util.*;
class Bouncy
{
/* true if every digit is greater than or equal to the digit before it, e.g. 1234, 14779 */
boolean isIncreasing(int num)
{
String str = String.valueOf(num);
for (int i = 1; i < str.length(); i++)
{
if (str.charAt(i) < str.charAt(i - 1))
return false;
}
return true;
}
/* true if every digit is less than or equal to the digit before it, e.g. 4321, 9731 */
boolean isDecreasing(int num)
{
String str = String.valueOf(num);
for (int i = 1; i < str.length(); i++)
{
if (str.charAt(i) > str.charAt(i - 1))
return false;
}
return true;
}
boolean isBouncy(int num)
{
return num > 0 && !isIncreasing(num) && !isDecreasing(num);
}
void printBouncy(int a, int b)
{
int count = 0;
for (int i = a; i <= b; i++)
{
if (isBouncy(i))
{
System.out.print(i + " ");
count++;
}
}
System.out.println();
System.out.println("Number of bouncy numbers: " + count);
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
Bouncy obj = new Bouncy();
System.out.println("Enter the range to print the bouncy numbers");
int p = sc.nextInt();
int q = sc.nextInt();
obj.printBouncy(p, q);
}
}Output:
Enter the range to print the bouncy numbers
100 130
101 102 103 104 105 106 107 108 109 120 121 130
Number of bouncy numbers: 12
Dry run for 1324
isIncreasing: 3 >= 1 yes, 2 >= 3 no -> not increasing
isDecreasing: 3 <= 1 no -> not decreasing
Neither increasing nor decreasing, so 1324 is bouncyA common mistake is to compare every digit with the first digit instead of the previous digit. Then 1324 looks increasing, because 3, 2 and 4 are all bigger than 1, and the program misses it.
Variable description
- num — int — the number being checked
- str — String — the digits of num as a String
- i — int — index of the current digit
- p, q — int — lower and upper limits of the range
- count — int — number of bouncy numbers found
Practice questions
- Print the first 20 bouncy numbers.
- Rewrite isIncreasing() without converting the number to a String, using % 10 and / 10.
- Find the percentage of bouncy numbers below 1000.
More number programs for ISC: the Goldbach number, Smith number and circular prime programs.