×

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

How to Convert int to char in Java

To convert a higher data type to lower data type, we need to do typecasting. Casting is also required when we want to convert ASCII value into character. It is...

3 minutes read.

Java Integer min() method

The min()  method of Integer class returns the smaller of two int values. It returns the same result as by calling Math.min.  Syntax public static int min(int a, int b) Parameters The parameters ‘a’...

2 minutes read.

Alien language problem in Java

Given the alphabetic sequence of an alien language, given a sorted dictionary (array of words) for the languages. Example: Words = { "aac", "abc", "aaa" } Output c, a, b Algorithm: (1) Compare two words that...

3 minutes read.

Java String toCharArray() method

Java String toCharArray() method converts current String into character array. Syntax: public char[] toCharArray() Returns: It returns a character array Java String toCharArray() method example 1 import java.util.Arrays; public class JavaStringToCharArrayEx1 {           public static...

2 minutes read.

Java Linters

When it comes to programming, everyone makes mistakes. Errors are bad for developers since they are difficult to handle. But handling as many as possible errors will bring out the...

6 minutes read.

Deadlock Prevention and avoidance in Java

This tutorial will discuss deadlock prevention and Avoidance in Java programming language. Introduction Multithreading in Java includes deadlock. We can multitask by running several threads concurrently in the multithreading environment. Deadlock occurs...

4 minutes read.

Kotlin Vs Java

Kotlin Vs Java There are many languages available for Android development. Java is the official language for android development but Kotlin is becoming popular nowadays. This article discusses both of these...

4 minutes read.

Java Applications

The growth in technology is increasing rapidly, so some languages are used for developing them. Java is one such famous programming language which is having numerous applications. The Java Programming...

4 minutes read.

Longest Odd Even Subsequence in Java

In order to solve the Java problem known as the longest odd-even subsequence, one must identify a sequences in a non-negative array having size s that alternately includes odd and...

6 minutes read.

How to Compare Two Strings in Java

How to Compare Two Strings in Java On the basis of reference or content, one can compare two strings in Java. String comparison is used in reference matching (== operator), sorting...

4 minutes read.

Program to find the duplicate characters in a string

Problem statement You have given with a string and your task is to find out the repeated characters from the string and print them. If no character is repeated, then you...

2 minutes read.

Callable Statement in Java

The Callable statement in Java is used to call the functions and Stored procedures. Example: If we want to know about the age of a person based on their date of birth,...

3 minutes read.

Blockchain in Java

Blockchain is a continuously expanding ledger that maintains an immutable, secure, and chronological record of all transactions that have ever occurred. It can be utilized to securely transfer money, assets,...

9 minutes read.

Uses of Java

Java is used in many real-world Java applications, including technologies and tools. This Java programming language has become the backbone for developing many applications. In areas like embedded systems and...

3 minutes read.

Java Integer parseUnsignedInt() method

The parseUnsignedInt() method of Java Integer class parses the string argument as an unsigned decimal integer. The second parameter parses the string argument as an unsigned integer in the radix specified...

2 minutes read.

Java String vs StringBuffer

Java String vs StringBuffer In this section, we will discuss the key differences between String and StringBuffer class. Before moving to the ahead in this section, let’s introduce with both classes. String...

4 minutes read.

List all files in a Directory in Java

The list of all files in the directory can be done using Java. You should be aware that a directory may include a subfolder and that subdirectory may also contain some...

3 minutes read.

Tetris Game in Java

The Tetris game is among the most well-known video games ever produced for computers. Today, we may engage in this game on a mobile device as well. Alexey Pajitnov conceptualized...

12 minutes read.

Can Abstract Classes have Static Methods in Java

Abstract Class An abstract class in Java is one that explicitly uses the keyword "abstract" in its declaration. There are options for both non-abstract and abstract techniques (method with the body)....

4 minutes read.

Java Command not found

Java Command not found error is displayed if Java is not installed on the computer or if the command prompt cannot find Java.exe to run the program. Our Java software...

3 minutes read.