In this tutorial you will learn about the Program to check Bouncy Number in Java and its application with practical example.
In this tutorial, we will learn to create a Java Program to check Bouncy Number in Java using Java programming.
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following Java programming topics:
- Java Operators.
- Basic Input and Output function in Java.
- Class and Object in Java.
- Basic Java programming. Basic Java programming.
- If-else statements in Java.
- For loop in Java.
Let’s understand first what is increasing and decreasing numbers before what is a Bouncy Number.
Increasing Number
Increasing number are number their value increases from left-to-right and no digit in right is smaller to its left it is called an increasing number.
example 234.
Decreasing Number
Decreasing number are number their value decrees from left-to-right and no digit in right is greater to its left it is called an decreasing number.
example 654.
Bouncy Number
A positive integer that is nor in increasing and nor in decreasing order is called a “bouncy number”
example. 165349
Program to check Bouncy Number in Java
In this program we will learn to create a program to check Given number is Bouncy Number or not. We would first declared and initialized the required variables. Next, we would prompt user to input a number and later we will find it is bouncy or not.Lets gave a look at the program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
import java.util.*; public class BouncyNumber { public static void main(String args[]) { Scanner scan = new Scanner(System.in); System.out.print("Enter any number: "); // taking value from user.. int number = scan.nextInt(); if (Increasing(number) || Decreasing(number) || number < 101) System.out.println(number+" is not a bouncy number."); else System.out.println(number+" is a bouncy number."); } //function that checks number is an increasing number or not public static boolean Increasing(int number) { //converts the number into string String str = Integer.toString(number); char digi; //flag set to true boolean flag = true; //iterates over the string up to length-1 for(int i=0;i < str.length()-1;i++) { digi = str.charAt(i); if(digi > str.charAt(i+1)) { flag = false; break; } } return flag; } //function that checks number is a decreasing number or not public static boolean Decreasing(int number) { String str = Integer.toString(number); char digit; //flag set to true boolean flag = true; //iterates over the string up to length-1 for(int i=0;i < str.length()-1;i++) { digit = str.charAt(i); if(digit < str.charAt(i+1)) { flag = false; break; } } return flag; } } |
Output
Bouncy Number
Not a Bouncy Number
In the above program, we have first declared and initialized a set variables required in the program.
Declaring a method Increasing(). It will checks the given number present in Increasing order or not.
Similarly another method named Decreasing() it will gives a number in present in Decreasing order or not.
After that we check if number not in Increasing order or nor in decreasing order or less than < 101 if any condition became true then the number is nit Bouncy number else number is Bouncy number.
So we can say that given number is Bouncy or not.