Bouncy Number in Java
A number is said to be a Bouncy number if the digits of the number are unsorted.
For example:
1728 is a bouncy number because the digits are unsorted.
2289 is not a bouncy number because the digits are sorted in ascending order.
74410 is not a bouncy number because the digits are sorted in descending order.
All numbers below 100 are not bouncy numbers.
Write a program in Java to accept a number and check whether it is a bouncy number or not.
import java.util.Scanner;
class Bouncy{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("N = ");
int n = Integer.parseInt(in.nextLine());
int len = String.valueOf(n).length();
int a[] = new int[len];
int j = 0;
for(int i = n; i != 0; i /= 10)
a[j++] = i % 10;
boolean asc = true;
for(int i = 0; i < a.length - 1; i++){
if(a[i] > a[i + 1]){
asc = false;
break;
}
}
boolean desc = true;
for(int i = 0; i < a.length - 1; i++){
if(a[i] < a[i + 1]){
desc = false;
break;
}
}
if(!asc && !desc)
System.out.println("Bouncy number!");
else
System.out.println("Not a Bouncy number.");
}
}
class Bouncy{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("N = ");
int n = Integer.parseInt(in.nextLine());
int len = String.valueOf(n).length();
int a[] = new int[len];
int j = 0;
for(int i = n; i != 0; i /= 10)
a[j++] = i % 10;
boolean asc = true;
for(int i = 0; i < a.length - 1; i++){
if(a[i] > a[i + 1]){
asc = false;
break;
}
}
boolean desc = true;
for(int i = 0; i < a.length - 1; i++){
if(a[i] < a[i + 1]){
desc = false;
break;
}
}
if(!asc && !desc)
System.out.println("Bouncy number!");
else
System.out.println("Not a Bouncy number.");
}
}
Comments
Post a Comment