Bouncy Number in Java

Bouncy Number in Java

Question : Write a program to check whether a number is Bouncy Number or not?

Bouncy Number : A positive integer that is neither in increasing nor decreasing number is called a bouncy number.
In other words, A number whose digits are unsorted.

Increasing Number : In an integer traversing from left to right if the current digit is greater than or equal to the previous digit, the number is known as increasing numbers.

Ex : 23689

Decreasing Number : In an integer traversing from left to right if the current digit is less than the previous digit, the number is known as decreasing numbers.

Ex : 98632

Example of Bouncy Number : 13174, 101, 15296 etc.

Note : There is no Bouncy Number between 1 to 100.


Note : To understand the program clearly watch this video : Bouncy Number in Java



Program to check Bouncy Number:-

Program :-

import java.io.*;
import java.util.*;
class Bouncy
{
    public static void main(String args[])
    {
        Scanner in = new Scanner(System.in);
        int n,p,a=0,b=0,x=0,y=0,d,sd;
        System.out.println("Enter Number");
        n=in.nextInt(); //Input Number
        p=n; //Copy of Input Number
        if(n<=100)
        {
            System.out.println("Hello! It is not a Bouncy Number : "+p);
        }
        else
        {
            while(n>9)
            {
                d=n%10;
                sd=(n%100)/10;
                n=n/10;
                if(((d>sd) || (d==sd)) && (b==0) && (y==0))
                {
                    a=1;
                    x=1;
                    continue;
                }
                else if((d<sd || d==sd) && (a==0) && (x==0))
                {
                    b=1;
                    y=1;
                    continue;
                }
                else
                {
                    b=2;
                    break;
                }
            }
            if(b==2)
            {
                 System.out.println("It is a Bouncy Number : "+p);
            }
            else
            {
                 System.out.println("It is not a Bouncy Number : "+p);
            }
        }
    }// end of main method
}// end of class


For Proper Understanding Watch the Video :-



Watch this video : Bouncy Number in Java.

All the best :)
Keep Learning :)

Comments

Popular posts from this blog

Composite Number in JAVA

Frequency of each digit of a number in Java

Fibonacci Prime Series in JAVA