Question
Print all the Bouncy numbers in a given range.
A bouncy number is one in which a non-negative integer is neither an increasing number nor a decreasing number.
An increasing number is a non-negative integer in which the digits from left to right are arranged in such a way that no digit on the left is greater than the digit to its right. For example, 1223
A decreasing number is a non-negative integer in which the digits from left to right are arranged in such a way that no digit on the left is smaller than the digit to its right. For example , 3221
Enter Lower Range
100
Enter Upper Range
120
Bouncy Numbers between 100 and 120:
101 102 103 104 105 106 107 108 109 1
Share code with your friends
Share on whatsapp
Share on facebook
Share on twitter
Share on telegram
Code
import java.util.Scanner;
public class BouncyNumberInRange
{
public static void main()
{
int temp=0,last=0,digit=0,lowerRange=0,upperRange=0,i=0;
boolean isIncreasing=true,isDecreasing=true;
Scanner sc=new Scanner(System.in);
System.out.println("Enter Lower Range");
lowerRange=sc.nextInt();
System.out.println("Enter Upper Range");
upperRange=sc.nextInt();
System.out.println("Bouncy Numbers between "+lowerRange+" and "+upperRange+":");
for(i=lowerRange;i<=upperRange;i++)
{
//Checking of number being negative is not required
//as it will not enter in while loop if number is negative integer
temp=i;
//This while loop is to check if the number is increasing number
last=temp%10;
while(temp>0)
{
temp=temp/10;
digit=temp%10;
if(last0)
{
isIncreasing=false;
break;
}
last=digit;
}
//This while loop is to check if the number is decreasing number
temp=i;
last=temp%10;
while(temp>0)
{
temp=temp/10;
digit=temp%10;
if(last>digit && temp>0)
{
isDecreasing=false;
break;
}
last=digit;
}
if(isDecreasing==false && isIncreasing==false)
{
System.out.print(i+" ");
}
isIncreasing=true;
isDecreasing=true;
}
}
}