Bouncy Number in Java
We will define bouncy numbers in this section and write Java programmes to determine whether a specific number is bouncy. Java coding exams and academic assignments usually inquire about the bouncy number programme. We shall first grasp what growing and decreasing numbers are before we can understand the bouncy number.
Increasing Numbers
If the current digit in an integer moving from the left to right is greater or comparable to the preceding digit, the number is said to be rising. In other terms, one can say that growing numbers are those in which no digit exceeds the digit immediately to its left. 1233, 13689, 112334566, etc. are a few examples.
The Decreasing Numbers
If somehow the current digit in an integer moving from the left to right is smaller than the preceding digit, the value is referred to as declining. In other terms, we may say that decreasing numbers are those in which no digit exceeds the digit immediately to its right. As an illustration, 321; 88531; 8755321; etc.
Now let's get to the bouncy number.
The Bouncy Number
A bouncing number is a positive number that is neither increasing nor decreasing. They oscillate between increasing and diminishing, in other words. In other words, if the number's digits are not sorted, we can assert that.
For instance, 101, 43682, 123742, etc. The digits of the above number, which are known as bouncing numbers, may be seen to be either rising nor decreasing as we move from left to right.
Note: Between 1 and 100, you'll notice that there are no bouncy numbers. 101 is the first bouncy number.
How to Locate a Bouncy Number?
- N can be read or initialised.
- Transform the provided number into a string, then save it in the variable str.
- Set a flag's initial value to true.
- Using a for loop, begin moving left to right characters at a time, and carry out the following:
- Create a method called isIncreasing (). Any digit that's also equal or greater to the following digit is checked. If so, change the flag's value to false and end the loop.
- Create a new method called isDecreasing (). It determines if any digit is smaller than the following one. If so, change the flag's value to false and end the loop.
- Give the flag variable's value back.
Create a method called isIncreasing (). Any digit that seems to be greater or comparable to the following digit is checked.
Bouncy Number Java Program
BouncyNumberDemo1.java
import java.util.*;
public class BouncyNumberDemo1
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number that you want to check: ");
// reading a user-provided integer
int inputNumb = sc.nextInt();
// If any of the subsequent tests yields true, the number is not bouncy.
if (isIncr(inputNumb) || isDecr(inputNumb) || inputNumb < 101)
// if the number is not bouncy it is printed
System.out.println(inputNumb+" is not a bouncy number.");
else
// if the number is bouncy it is printed
System.out.println(inputNumber+" It is a bouncy number.");
}
// function that determines whether or not a given number is rising.
public static boolean isIncr(int inputNumb)
{
// number is converted to string
String sr = Integer.toString(inputNumb);
char digits;
//the flag is set to true
boolean flag = true;
// repeats the string until it reaches len1
for(int s=0;s < sr.len()-1;s++)
{
digits = sr.charAt(s);
// If any digit exceeds the next digit being checked, it will stop checking.
if(digits > sr.charAt(s+1))
{
// If the test results in a true value, the flag is set to false.
flag = false;
break;
}
}
return flag;
}
// function that determines whether a given number is decreasing or not.
public static boolean isDecr(int inputNumb)
{
// makes a string out of the number.
String sr = Integer.toString(inputNumb);
char digits;
//the flag is set to true
boolean flag = true;
// repeats the string until length-1 has been reached.
for(int s=0;s < sr.len()-1;s++)
{
digits = sr.charAt(s);
// It will stop checking whether any digit is smaller than the subsequent digit.
if(digits < sr.charAt(s+1))
{
// If the test results in a true value, the flag is set to false.
flag = false;
break;
}
}
return flag;
}
}
Output:
Enter the number that you want to check: 123742
123742 It is a bouncy number.