Java Program To Check Bouncy Number Increasing Decreasing Number
Chapter:
Math Class
Last Updated:
07-02-2018 15:08:15 UTC
Program:
/* ............... START ............... */
import java.util.*;
public class JavaBouncyNumber {
boolean isIncreasing(int number)
{
String s = Integer.toString(number);
char ch;
int f = 0;
for (int i = 0; i < s.length() - 1; i++) {
ch = s.charAt(i);
if (ch > s.charAt(i + 1))// If any digit is more than next digit
// then we have to stop checking
{
f = 1;
break;
}
}
if (f == 1)
return false;
else
return true;
}
boolean isDecreasing(int number)
{
String s = Integer.toString(number);
char ch;
int f = 0;
for (int i = 0; i < s.length() - 1; i++) {
ch = s.charAt(i);
if (ch < s.charAt(i + 1))// If any digit is less than next digit
// then we have to stop checking
{
f = 1;
break;
}
}
if (f == 1)
return false;
else
return true;
}
void isBouncy(int number) {
if (isIncreasing(number) == true)
System.out.println("The number " + number + " is Increasing and Not Bouncy");
else if (isDecreasing(number) == true)
System.out.println("The number " + number + " is Decreasing and Not Bouncy");
else
System.out.println("The number " + number + " is bouncy");
}
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
JavaBouncyNumber bouncyNumber = new JavaBouncyNumber();
System.out.print("Enter a number : ");
int number = scanner.nextInt();
bouncyNumber.isBouncy(number);
}
}
/* ............... END ............... */
Output
Enter a number : 22344
The number 22344 is Increasing and Not Bouncy
Enter a number : 774410
The number 774410 is Decreasing and Not Bouncy
Enter a number : 155349
The number 155349 is bouncy
Notes:
-
Working from left-to-right if no digit is exceeded by the digit to its left it is called an increasing number, for example : 134468.
- Similarly if no digit is exceeded by the digit to its right it is called a decreasing number, for example : 66420.
- A positive integer that is neither increasing nor decreasing is a bouncy number. for example : 155349.
- Clearly there cannot be any bouncy numbers below one-hundred, but just over half of the numbers below one-thousand (525) are bouncy. In fact, the least number for which the proportion of bouncy numbers first reaches 50% is 538.
Tags
Java Program To Check Bouncy Number Increasing Decreasing Number,
Area Of Circle, Java, Math