×

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:
    1. 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.
    2. 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.

Related Topics

Java Heap Space Out of Memory Error

This error is called an "out of memory" error, which indicates that the JVM cannot allocate an object in memory from the heap. Hence the java.lang.out of memory error, describing...

2 minutes read.

How to add Elements in Array in Java

Consider an array of the size n, in the given size we must add the elements in the given array. In this tutorial we are preferred to learn how to add...

3 minutes read.

Java Integer floatValue() method

The floatValue() method of Integer class returns a float value for this Integer after a widening primitive conversion. Syntax public float floatValue() Parameters NA Specified by This method is specified by floatValue in class Number Return Value This...

1 minute read.

Java Read File Line by line

Java provides two options to read files line by line. Each option offers different benefits and has its own set of drawbacks. Following are the ways through which on accomplish...

5 minutes read.

Stable Marriage Problem in Java

Given N men and N women, the Stable Marriage Problem asks you to match up the men and women in such a way that there are never any two people...

6 minutes read.

Convert JSON File to String in Java

Before understanding the conversion of JSON file to string, one must know about JSON. What is JSON? JSON stands for JavaScript Object Notation. It is an open standard format lightweight, text-based, and...

3 minutes read.

Local Minima in Java

An Array Finding a local minimum in an array a[0. m-1] of different integers is the job. A[i] is considered a local minimum if it is smaller than two of its...

4 minutes read.

Can we override Private Method in Java

In this article we will learn the concept of override, private method in java and we will now whether we can override private method in java or not. Override in Java Any...

3 minutes read.

What is the ambiguity problem in Java?

The ambiguity problem in Java occurs when a method or constructor is overloaded with two or more methods with the same name but different parameters. This can confuse when multiple...

6 minutes read.

Segment Tree in Java

Binary trees can address a variety of issues; however, the Segment Tree is more efficient in terms of time complexity. The segment tree in Java is represented using an array. Native...

4 minutes read.

Java 8 Multimap

Java comes with several practical built-in collection libraries. However, there are situations when we need specialized collections that are not included in the Java standard library. The Multimap is one...

7 minutes read.

Sales Tax Problem in Java

To calculate the sales tax on a purchase in Java, you will need to know the following information: The cost of the item being purchased The sales tax rate You can then use...

4 minutes read.

Convert list to array Java

One of the popular collection interfaces for storing an ordered collection is the List. The List interface may contain repeating groups and preserves the insertion order of entries. This article will...

4 minutes read.

String Pool in Java

String Pool in Java: String is one of the most important discussed topics in Java. There are a lot of concepts related to the String and one of them is...

5 minutes read.

Java Math cosh() Method

The cosh() method of Math class returns the first hyperbolic cosine((e+e)/2) of a double value. Syntax: public static double cosh(double x) Parameters: The parameter ‘x’ represents the number whose hyperbolic cosine is to be...

2 minutes read.

Exception Handling Program in Java

Exception Handling Program in Java Exception means something that is abnormal. In Java, an exception is treated as a problem that disrupts the normal flow of the program. An exception leads...

7 minutes read.

Java Boolean equals() method

The equals() method of Java Boolean class returns a Boolean value true if the specified argument is not null and is same as this object, else it returns false. Syntax public boolean...

2 minutes read.

Even Odd Program in Java

Even Odd Program in Java The number that are completely divisible by 2 are even and the number that leaves remainder are odd numbers. For example, the numbers 2, 0, 4,...

5 minutes read.

Best Java Security Framework

The security of applications is currently our top concern when creating them. The applications or bits of code running over the network are exposed to dangers and may jeopardize integrity,...

3 minutes read.

Java Integer sum() method

The sum() method of Java Integer class add the two specified integers values. It returns the same result as given by + operator. Syntax public static int sum (int a, int b)  Parameters The...

1 minute read.