# Bouncy Number in Java: Exploring Number Patterns

(Last Updated On: 31/08/2023)

In this section, we will define a bouncy number and write Java programs to determine whether a given number is bouncy. The bouncy number program comes up frequently in Java coding tests and academics. Before we can understand the bouncy number, we must first understand what increasing and decreasing numbers are.

Numbers are essential in mathematics and many real-world applications. They have distinct patterns and characteristics that fascinate both mathematicians and programmers. The concept of “bouncy numbers” is one such intriguing concept. In this blog, we will explore the world of bouncy numbers, learn about their properties, and discover how to identify them using Java programming.

## What is the Bouncy Number in Java?

When read from left to right, bouncy numbers have a change in their digit pattern. They show a change from increasing to decreasing digits or vice versa. For example, 123 is not a bouncy number, whereas 321 is because the digit order is reversed.

### Bouncy Number Examples:

Let’s look at some examples to better understand bouncy numbers. The number 45678 is bouncy because it begins with an increasing sequence (4, 5, 6, 7, 8), followed by a decreasing sequence. 12345, on the other hand, has an increasing pattern and thus is not a bouncy number.

In Java, a bouncy number is one whose digits either increase or decrease in value, but not in a strictly monotonic manner. Here are three examples of bouncy numbers in Java:

1. Example: 9641

The digits of the number decrease from left to right (9 > 6 > 4), then increase (4 < 1).

1. Example: 13579

This digits of the number increase from left to right (1 < 3 < 5 < 7 < 9).

1. Example: 111111

The digits of the number are all the same, so it is considered a bouncy number.

These examples show various bouncy number patterns where the digits mix increasing, decreasing, or repeated sequences.

Get a Complete list of Online Certification Courses in India here!

## The given code is a Java program that checks whether a given number is a “Bouncy” number or not by Using a function.

```import java.util.*;

public class bouncynumEx {
public static void main(String args[]) {
System.out.println("Enter any number : ");
Scanner scan = new Scanner(System.in);
int inputNum = scan.nextInt();
if (isIncreasing(inputNum) || isDecreasing(inputNum)
|| inputNum < 101)
System.out.println(inputNum+" Not a Bouncy number");
else
System.out.println(inputNum+" is a Bouncy number");
}

public static boolean isIncreasing(int inputNum) {
// Converting inputNumber to String
String str = Integer.toString(inputNum);
char digit;
boolean flag = true;
for(int i=0;i < str.length()-1;i++) {
digit = str.charAt(i);
/*if any digit is greater
than next digit, Stop checking further*/
if(digit > str.charAt(i+1)) {
flag = false;
break;
}
}
return flag;
}

public static boolean isDecreasing(int inputNum) {
// Converting inputNumber to String
String str = Integer.toString(inputNum);
char digit;
boolean flag = true;
for(int i=0;i < str.length()-1;i++) {
digit = str.charAt(i);
/*if any digit is lesser
than next digit, Stop checking further*/
if(digit < str.charAt(i+1)) {
flag = false;
break;
}
}
return flag;
}
}```

Explanation of the code:

A “Bouncy” number is a number whose digits are not strictly increasing or decreasing. Here’s an explanation of the code:

The main method is the entry point of the program. It prompts the user to enter a number and reads the input using a Scanner object.

The program then calls two helper methods: isIncreasing and isDecreasing, passing the input number as an argument. These methods check whether the number is increasing or decreasing, respectively.

The isIncreasing method takes an integer input and converts it to a string. It iterates over the digits of the number using a for loop and compares each digit with the next one. If any digit is greater than the next digit, the method sets the flag variable to false and breaks out of the loop. Otherwise, it remains true.

The isDecreasing method is similar to isIncreasing, but it checks if any digit is lesser than the next digit.

After calling the helper methods, the program checks if any of the conditions are met to determine if the number is not a Bouncy number. If the number is increasing, decreasing, or less than 101, it prints a corresponding message indicating that the number is not a Bouncy number. Otherwise, it prints that the number is a Bouncy number.

Also, read our blog on ISBN Number in Java!

#### Output:

``````Enter any number : 6549
6549 is a Bouncy number``````

## Check whether a given number is a “Bouncy” number or not by using variable

```import java.util.Scanner;
public class BouncyNumEx
{
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.");
}
} ```

Explanation of the code:

The main method is the program’s starting point. It asks the user to enter a number and reads it with a Scanner object. The program then determines whether the entered number is less than 100. If it is, it prints that the number is not a Bouncy number and exits the method. The program creates variables such as t to hold the entered number, isIncreasing and isDecreasing as boolean flags and prev to hold the previous digit of t.

The program goes into two while loops. In each loop, the modulus operator is used to extract the last digit of t and compare it to the previous digit (prev). If a digit in the first loop is greater than the previous one in the second loop, the corresponding flag (isIncreasing or isDecreasing) is set to false, and the loop is exited. Following the loops, the program determines whether both flags (isIncreasing and isDecreasing) are false. If they are, the number is printed as a Bouncy number. Otherwise, the number is printed as not being a Bouncy number.

#### Output:

``````Enter a number: 52
52 is not a Bouncy Number.``````

### Algorithm for Bouncy Number

1. Convert the inputNumber to a String and store the result in the variable str.

2. Set the boolean variable flag to true. The final value of the flag variable determines whether the number is bouncy or not.

3. Making use of a for loop a. Work your way from left to right. b. In the isIncreasing() method, determine whether any digit is greater than the next digit. If yes, set the flag to false and exit the for loop. c. In the isDecreasing() method, determine whether any digit is less than the next digit. If yes, set the flag to false and exit the for loop.

4. Get the value of the flag variable.

Understand the Concept of Krishnamurthy Number in Java, Here!

### 10 Interesting Facts about Bouncy Number in Java

1. A fascinating mathematical concept known as “bouncy numbers” describes the characteristics of numbers when their digits alternate between increasing and decreasing sequences.
2. By making a number into a string in Java, you can check for the desired alternating pattern while iterating through the digits to see if the number is bouncy.
3. In some mathematical literature, “hilly numbers” or “de croissant numbers” are other names for bouncy numbers.
4. The first couple of bouncy numbers are 121, 212, 232, 323, 343 and so on. Numbers that are strictly increasing or strictly decreasing are not included.
5. As the numbers’ magnitude rises, the proportion of bouncy numbers falls. In higher ranges, bouncy numbers actually become less common.
6. There are some intriguing connections between probability theory, combinatorics, and number theory, and bouncy numbers.
7. Data analysis, pattern recognition, and cryptography are just a few of the areas where bouncing numbers are used.
8. Using dynamic programming or other algorithms, it is possible to efficiently calculate the total number of bouncy numbers below a specific threshold.
9. When plotted on graphs or represented differently, bouncy numbers display intriguing patterns and behaviors.
10. By delving into number manipulation, algorithmic thinking, and the beauty of mathematical concepts, exploring bouncy numbers in Java can be both a fun and educational exercise.

In this blog, we explored the fascinating concept of bouncy numbers in Java and learned how to identify them using Java programming. We understood the definition and characteristics of bouncy numbers and discussed their digit patterns. By implementing an algorithm in Java, we demonstrated how to detect bouncy numbers efficiently. We also explored optimization techniques and advanced approaches for handling large numbers. By understanding number patterns and their significance, we can broaden our problem-solving skills and explore further applications in various domains. So, grab your Java compiler and start exploring the world of bouncy numbers!
We hope you found our blog on “Bouncy Number in Java” informative and helpful. For more programming-related blogs and courses, visit our website Newtum and keep learning with us!