# How To Check Whether a Number Is a Bouncy Number or Not in Java?

Bouncy number can be defined as a positive number whose digits do not follow increasing or decreasing order.

Means the digits are in an unsorted manner.

There are no bouncy numbers between 1 to 100.

## To show you some instances

### Instance-1

Input number is 101

Let’s check it by using the logic of Bouncy number −

1 > 0 and 0 < 1


Hence, 101 is a Bouncy number.

### Instance-2

Input number is 905

Let’s check it by using the logic of Bouncy number −

9 > 0 and 0 < 5


Hence, 905 is a Bouncy number.

### Instance-3

Input number is 245

Let’s check it by using the logic of Bouncy number −

2 < 4 but 4 < 5 means all digits are in decreasing order.


Hence, 245 is not a Bouncy number.

Some other examples of Bouncy numbers include 102, 564,897 etc.

## Syntax

We can convert integer value to String value by using inbuilt toString() method.

Following is the syntax for that.

String str = Integer.toString(inputNumber)


We can pick a character at any index from a string using charAt( ) method. Following is the syntax for that.

char ch = str.charAt(0);


## Algorithm

• Step 1 − Get an integer number either by initialization or by user input.

• Step 2 − Set flag to true and check if the digits are in ascending or descending order

• Step 3 − If the digits follow ascending or descending order set flag to false.

• Step 4 − Print the result.

## Multiple Approaches

We have provided the solution in different approaches.

• By Using Static Input Value

• By Using User Defined Method and with charAt( ) Method

Let’s see the program along with its output one by one.

### Approach-1: By Using Static Input Value

In this approach, an integer value will be initialized in the program and then by using the algorithm we can check whether a number is a Bouncy number or not.

#### Example

import java.util.*;

public class Main{
//Main method
public static void main(String args[]){

//initialized a number
int num = 6549;

//printing the given number
System.out.println("Given number: "+num);

// Checks if the number is less than 100
if(num<100){
System.out.println("The number is not a bouncy number");
}

// Boolean values to check if the digits are sorted
boolean increasing = true, decreasing = true;

// Storing the number in a temporary variable and storing the first digit
int temp = num, prev = num % 10;

// Loops until the number becomes zero
while(temp != 0){

// Checks if it follows increasing order or not
if(temp%10>prev){
increasing = false;
break;
}

// Gets the last digit of the number
prev = temp % 10;

// Removes the last digit
temp /= 10;
}

// Resets the value
temp = num;
prev = num % 10;

while(temp != 0){
// Checks if it follows decreasing order or not
if(temp%10<prev){
decreasing = false;
break;
}

// Gets the last digit of the number
prev = temp % 10;

// Removes the last digit
temp /= 10;
}

// Prints the result
if(!increasing&&!decreasing)
System.out.println("The number is a bouncy number");
else
System.out.println(", The number is not a bouncy number");
}
}


#### Output

Given number: 6549
The number is a bouncy number

.

### Approach-2: By Using User Defined Method and With charAt() Method

In this approach, the user will be asked to take the input of an integer value and pass this number as a parameter in a user defined method then inside the method by using the algorithm we can check whether a number is a Bouncy number or not.

#### Example

public class Main{
// Checks if the digits are in increasing order
static boolean isIncreasing(int num){

// Converts the number to a string
String s = Integer.toString(num);
char dig;
boolean result = true;

// Loops over all the digits
for(int i = 0; i < s.length()-1; i++){

// Stores the digit at i th location
dig = s.charAt(i);

// Compares the digit with its next location
if(dig > s.charAt(i+1)){

// Sets the result to false and breaks out of the loop
result = false;
break;
}
}
return result;
}

// Checks if the digits are in decreasing order
static boolean isDecreasing(int num){

// Converts the number to a string
String s = Integer.toString(num);
char dig;
boolean result = true;

// Loops over all the digits
for(int i = 0; i < s.length()-1; i++){

// Stores the digit at i th location
dig = s.charAt(i);

// Compares the digit with its next location
if(dig < s.charAt(i+1)){

// Sets the result to false and breaks out of the loop
result = false;
break;
}
}
return result;
}
//user defined method to check bouncy number
static boolean bouncyNum(int num){

// Checks if the number is less than 100
if(num<100){

System.out.println("The number is not a bouncy number");
}
// Checks for sorted digits using the functions
if(!isIncreasing(num)&&!isDecreasing(num))
return true;
else
return false;
}
//main method
public static void main(String args[]){

//initialized a number
int num = 150;

//printing the given number
System.out.println("Given number: "+num);

// Calls the user defined method and stores the result in res variable
boolean res = bouncyNum(num);

// Prints the result
if(res)
System.out.println("The number is a bouncy number");
else
System.out.println("The number is not a bouncy number");
}
}


#### Output

Given number: 150
The number is a bouncy number


In this article, we explored how to check a number whether it is a bouncy number or not in Java by using different approaches.