Question
Check whether the number is Bouncy number or not using only main method.
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 the number
1231
1231 is a Bouncy Number
Here 1231 is neither increasing number nor decreasing number so it is a bouncy number.
Share code with your friends
Share on whatsapp
Share on facebook
Share on twitter
Share on telegram
Code
import java.util.Scanner;
public class BouncyNumber
{
public static void main()
{
int num=0,temp=0,last=0,digit=0;
boolean isIncreasing=true,isDecreasing=true;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number");
num=sc.nextInt();
//Checking of number being negative is not required
//as it will not enter in while loop if number is negative integer
temp=num;
last=temp%10;
//This while loop is to check if the number is increasing number
while(temp>0)
{
temp=temp/10;
digit=temp%10;
if(last0)
{
isIncreasing=false;
break;
}
last=digit;
}
temp=num;
last=temp%10;
//This while loop is to check if the number is decreasing number
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.println(num+" is a Bouncy Number");
}
else
{
System.out.println(num+" is not a Bouncy Number");
}
}
}