In this section, we will explore the concept of bouncy numbers and create Java programs to determine whether a given number is bouncy. The topic of bouncy numbers often appears in Java coding tests and academic assignments. To grasp the concept of bouncy numbers, it is essential to first understand increasing and decreasing numbers.
Increasing Numbers in Java
When traversing an integer from left to right, if the current digit is greater than or equal to the previous digit, the number is classified as an increasing number. Another way to define it is that an increasing number is one where no digit is surpassed by the digit to its left. For instance, examples of increasing numbers include 1233, 13689, and 112334566.
Decreasing Numbers in Java
When examining an integer from left to right, if the current digit is smaller than the previous digit, the number is classified as a decreasing number. Alternatively, a decreasing number can be defined as a number where no digit is surpassed by the digit to its right. Examples of decreasing numbers include 321, 88531, and 8755321.
What is a Bouncy Number?
A bouncy number is a number that is neither strictly increasing nor strictly decreasing. In other words, it is a number that exhibits a mix of ascending and descending digits when its digits are observed from left to right. For example, the number 538 is a bouncy number because the digits increase from 5 to 8 and then decrease from 8 to 3. On the other hand, the number 724 is not a bouncy number because its digits strictly increase from 7 to 2, without any descending digits.
Steps to Find Bouncy Number
- Read or assign a value to the variable N.
- Convert the given number N into a string and store it in the variable named “str.”
- Initialize a boolean variable named “flag” with the value true.
- Begin iterating over the characters of the string from left to right using a for loop and perform the following steps:
- Create a method named “isIncreasing” that checks if any digit is greater than or equal to the next digit. If this condition is met, set the flag variable to false and exit the loop.
- Create another method named “isDecreasing” that checks if any digit is less than the next digit. If this condition is met, set the flag variable to false and exit the loop.
- Return the value of the flag variable.
Bouncy Number Java Program
BouncyNumberExample.java
import java.util.*; public class BouncyNumberExample { public static void main(String args[]) { Scanner scan = new Scanner(System.in); System.out.print("Enter any number you want to check: "); //reading an integer from the user int inputNumber = scan.nextInt(); //if any of the following condition returns true, the number id not bouncy if (isIncreasing(inputNumber) || isDecreasing(inputNumber) || inputNumber < 101) //prints if the number is not bouncy System.out.println(inputNumber+" not a bouncy number."); else //prints if the number is bouncy System.out.println(inputNumber+" is a bouncy number."); } //function that checks if the number is an increasing number or not public static boolean isIncreasing(int inputNumber) { //converts the number into string String str = Integer.toString(inputNumber); char digit; //flag set to true boolean flag = true; //iterates over the string up to length-1 for(int i=0;i < str.length()-1;i++) { digit = str.charAt(i); //if any digit is greater than check next digit, it will not check further if(digit > str.charAt(i+1)) { //flag set to false if the condition returns true flag = false; break; } } return flag; } //function that checks if the number is a decreasing number or not public static boolean isDecreasing(int inputNumber) { //converts the number into string String str = Integer.toString(inputNumber); char digit; //flag set to true boolean flag = true; //iterates over the string up to length-1 for(int i=0;i < str.length()-1;i++) { digit = str.charAt(i); //if any digit is less than the next digit, it will not check further if(digit < str.charAt(i+1)) { //flag set to false if the condition returns true flag = false; break; } } return flag; } }
Output 1:
Enter any number you want to check: 12574 12574 is a bouncy number.
Output 2:
Enter any number you want to check: 200 200 not a bouncy number.
BouncyNumberExample1.java
import java.util.Scanner; public class BouncyNumberExample1 { public static void main(String args[]) { int n; Scanner in = new Scanner(System.in); System.out.print("Enter a number: "); //reading an integer from the user n = in.nextInt(); //checks if the number is less than 100 or not if (n < 100) { //if yes, prints not bouncy number System.out.println(n + " is not a Bouncy Number."); return; } //assigning the given number into a variable int t = n; boolean isIncreasing = true, isDecreasing = true; int prev = t % 10; while (t != 0) { int d = t % 10; if (d > prev) { isIncreasing = false; break; } prev = d; t = t/10; } t = n; prev = t % 10; while (t != 0) { int d = t % 10; if (d < prev) { isDecreasing = false; break; } prev = d; t = t/10; } //returns true if both conditions return true if (!isIncreasing && !isDecreasing) System.out.println(n + " is a bouncy number."); else System.out.println(n + " is not a bouncy number."); } }
Output 1:
Enter a number: 12541 12541 is a bouncy number.
Output 2:
Enter a number: 400 400 is not a bouncy number.
Ensure that you explore our tutorials.freshersnow.com portal to discover additional concepts similar to Bouncy Number in Java.