Java Program to Check Bouncy Number

In the previous article, we have discussed Java Program to Check Krishnamurthy Number

In this article we are going to understand what Bouncy number is and how we can check whether a number is Bouncy or not in Java with examples.

Program to Check Bouncy Number

Bouncy numbers are numbers whose digits are not all increasing sequence or all decreasing sequence, the sequence bounces from increasing to decreasing. Naturally these numbers should be three digit numbers.

 Example :

145: 1<4<5 Not a Bouncy number
192: 1<9>2 Bouncy number
241: 2<4>1 Bouncy number

In the above examples the numbers 192 and 241 are Bouncy numbers as their digits keeps  on increasing and decreasing from the previous ones. However 145 is in an increasing sequence hence it is not a bouncy number.

The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples as it includes basic to advanced levels of concepts.

Let’s see different approaches:

  • By Using Static Value
  • By User Input Value
  • Approach :

    1. We ask the user to enter a number and store it .
    2. The digits are iterated and are checked if they are in a increasing or decreasing sequence.
    3. If the sequence is neutral (and the digits are greater than 101), then the number is said to be a Bouncy number.

    Method-1: Java Program to Check Bouncy Number By Using User Static Value

    import java.util.Scanner;
    public class BouncyNumber{
        public static void main(String args[])
        {
            //A number declared
            int num = 241;
    
            // Checks if the digits are in increasing or decreasing sequence, and if they are three digits
            // 100 is not a bouncy nimber as its digits are in decreasing order, hence it is not checked
            if(isDecreasing(num)||isIncreasing(num)||num<101)
            {
                System.out.println(num+" is Not a Bouncy number");
            }
            else
            {
                System.out.println(num+" is a Bouncy number");
            }
        }
    
        // Function that checks whether the number is in increasing sequence
        public static boolean isIncreasing(int num)   
        {  
            String str = Integer.toString(num);  
            char digit;
            boolean flag = true;  
            //iterates over the string  
            for(int i=0;i < str.length()-1;i++)   
            {  
                digit = str.charAt(i);  
                //if any digit is greater then check next digit
                if(digit > str.charAt(i+1))   
                {     
                flag = false;  
                break;  
                }      
            }  
            return flag;  
        }  
        
        // Function that checks whether the number is in decreasing sequence 
        public static boolean isDecreasing(int num)   
        {
            String str = Integer.toString(num);  
            char digit;
            boolean flag = true; 
            for(int i=0;i < str.length()-1;i++)   
            {  
            digit = str.charAt(i);  
            //if any digit is less than the next digit
            if(digit < str.charAt(i+1))   
            {      
            flag = false;  
            break;  
            }      
        }  
        return flag;          
        }  
    }
    Output:
    
    241 is a Bouncy number

    Method-2: Java Program to Check Bouncy Number By Using User Input Value

    import java.util.Scanner;
    public class BouncyNumber{
        public static void main(String args[])
        {
            //Taking the number as input from the user using scanner class
            Scanner scan = new Scanner(System.in);
            System.out.print("Enter a number : ");
            int num = scan.nextInt();
    
            // Checks if the digits are in increasing or decreasing sequence, and if they are three digits
            // 100 is not a bouncy nimber as its digits are in decreasing order, hence it is not checked
            if(isDecreasing(num)||isIncreasing(num)||num<101)
            {
                System.out.println(num+" is Not a Bouncy number");
            }
            else
            {
                System.out.println(num+" is a Bouncy number");
            }
        }
    
        // Function that checks whether the number is in increasing sequence
        public static boolean isIncreasing(int num)   
        {  
            String str = Integer.toString(num);  
            char digit;
            boolean flag = true;  
            //iterates over the string  
            for(int i=0;i < str.length()-1;i++)   
            {  
                digit = str.charAt(i);  
                //if any digit is greater then check next digit
                if(digit > str.charAt(i+1))   
                {     
                flag = false;  
                break;  
                }      
            }  
            return flag;  
        }  
        
        // Function that checks whether the number is in decreasing sequence 
        public static boolean isDecreasing(int num)   
        {
            String str = Integer.toString(num);  
            char digit;
            boolean flag = true; 
            for(int i=0;i < str.length()-1;i++)   
            {  
            digit = str.charAt(i);  
            //if any digit is less than the next digit
            if(digit < str.charAt(i+1))   
            {      
            flag = false;  
            break;  
            }      
        }  
        return flag;          
        }  
    }   
    
    
    Output:
    
    Case-1
    
    Enter a number : 241
    241 is  a Bouncy number
    
    Case-2
    
    Enter a number : 146
    146 is Not a Bouncy number

    Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples and try to code all of them on your own then check with the exact code provided by expert programmers.

    Related Java Programs: