# Bouncy Number in Java

In this section, we will learn what is a bouncy number and also create Java program to check if the given number is bouncy. The bouncy number program frequently asked in Java coding tests and academics. Before understanding the bouncy number, first, we will understand what is increasing and decreasing numbers.

## Increasing Numbers

In an integer traversing from left to right if the current digit is greater than or equal to the previous digit, the number is known as increasing numbers. In other words, we can say that if no digit is exceeded by the digit to its left is called increasing numbers. For example, 1233, 13689, 112334566, etc.

## Decreasing Numbers

In an integer traversing from left to right if the current digit is less than the previous digit, the number is known as decreasing numbers. In other words, we can say that if no digit is exceeded by the digit to its right is called decreasing numbers. For example, 321, 88531, 8755321, etc.

Let’s move to the bouncy number.

## Bouncy Number

A positive integer that is neither in increasing nor decreasing number is called a bouncy number. It means they bounce between increasing and decreasing. In other words, we can say that if the digits of the number are unsorted.

For example, 123742, 101, 43682, etc. We observe that in the given number’s digits are neither increasing nor decreasing if we traverse from left to right, hence they are called bouncy numbers.

## Steps to Find Bouncy Number

1. Read or initialize a number N.
2. Convert the given number into a string and store the string in variable str.
3. Initialize a flag with true value.
4. Start moving from left to right character by character by using a for loop and do the following:
• Define a method with the name isIncreasing(). It checks if any digit is greater than or equal to the next digit. If yes, set the flag to false and exit from the loop.
• Define another method with the name isDecreasing(). It checks if any digit is less than the next digit. If yes, set the flag to false and exit from the loop.
5. Return the value of the flag variable.

Let’s implement the above steps in the Java program and check whether the given number is bouncy or not.

## Bouncy Number Java Program

BouncyNumberExample1.java

1. import java.util.*;
2. public class BouncyNumberExample1
3. {
4. public static void main(String args[])
5. {
6. Scanner scan = new Scanner(System.in);
7. System.out.print(“Enter any number you want to check: “);
8. //reading an integer from the user
9. int inputNumber = scan.nextInt();
10. //if any of the following condition returns true, the number id not bouncy
11. if (isIncreasing(inputNumber) || isDecreasing(inputNumber) || inputNumber < 101)
12. //prints if the number is not bouncy
13. System.out.println(inputNumber+” not a bouncy number.”);
14. else
15. //prints if the number is bouncy
16. System.out.println(inputNumber+” is a bouncy number.”);
17. }
18. //function that checks if the number is an increasing number or not
19. public static boolean isIncreasing(int inputNumber)
20. {
21. //converts the number into string
22. String str = Integer.toString(inputNumber);
23. char digit;
24. //flag set to true
25. boolean flag = true;
26. //iterates over the string up to length-1
27. for(int i=0;i < str.length()-1;i++)
28. {
29. digit = str.charAt(i);
30. //if any digit is greater than check next digit, it will not check further
31. if(digit > str.charAt(i+1))
32. {
33. //flag set to false if the condition returns true
34. flag = false;
35. break;
36. }
37. }
38. return flag;
39. }
40. //function that checks if the number is a decreasing number or not
41. public static boolean isDecreasing(int inputNumber)
42. {
43. //converts the number into string
44. String str = Integer.toString(inputNumber);
45. char digit;
46. //flag set to true
47. boolean flag = true;
48. //iterates over the string up to length-1
49. for(int i=0;i < str.length()-1;i++)
50. {
51. digit = str.charAt(i);
52. //if any digit is less than the next digit, it will not check further
53. if(digit < str.charAt(i+1))
54. {
55. //flag set to false if the condition returns true
56. flag = false;
57. break;
58. }
59. }
60. return flag;
61. }
62. }

Output 1:

Output 2:

BouncyNumberExample2.java

1. import java.util.Scanner;
2. public class BouncyNumberExample2
3. {
4. public static void main(String args[])
5. {
6. int n;
7. Scanner in = new Scanner(System.in);
8. System.out.print(“Enter a number: “);
9. //reading an integer from the user
10. n = in.nextInt();
11. //checks if the number is less than 100 or not
12. if (n < 100)
13. {
14. //if yes, prints not bouncy number
15. System.out.println(n + ” is not a Bouncy Number.”);
16. return;
17. }
18. //assigning the given number into a variable
19. int t = n;
20. boolean isIncreasing = true, isDecreasing = true;
21. int prev = t % 10;
22. while (t != 0)
23. {
24. int d = t % 10;
25. if (d > prev)
26. {
27. isIncreasing = false;
28. break;
29. }
30. prev = d;
31. t = t/10;
32. }
33. t = n;
34. prev = t % 10;
35. while (t != 0)
36. {
37. int d = t % 10;
38. if (d < prev)
39. {
40. isDecreasing = false;
41. break;
42. }
43. prev = d;
44. t = t/10;
45. }
46. //returns true if both conditions return true
47. if (!isIncreasing && !isDecreasing)
48. System.out.println(n + ” is a bouncy number.”);
49. else
50. System.out.println(n + ” is not a bouncy number.”);
51. }
52. }

Output 1:

Output 2: