Bouncy Number

 //Write a pro gram to accept a number and check it is a bouncy number or not.

//Bouncy number- A number which is neither increasing or decreasing is known as Bouncy Number.ex-16237

//Increasing number- A number which is increasing from left to right.ex-2459

//Decreasing number - A number which is increasing from right to left.ex-8631

import java.util.*;

class bouncy

{//class bouncy opens

    public static void main(String[]args)

    {//main method opens

        Scanner sc=new Scanner(System.in);

        System.out.println("Enter a no");

        int n=sc.nextInt();//variable n to accept the no - 2459

        String k=Integer.toString(n);//variable k to convert the no into String form -"2459"

        int len=k.length();//variable len to find the length - 4

        int k1=n,c=0,c1=0,r=0,s=0,min=0,max=0;//variable k1 to store the no,variable r to extract the digits

        while(k1>0)//2459>0 t//245>0 t//24>0 t//2>0 t//0>0 f

        {

            r=k1%10;//2459%10=9//245%10=5//24%10=4//2%10=2

            s=s*10+r;//variable s to reverse the no//0x10+9=9//9x10+5=95//95x10+4=954//954x10+2=9542

            k1=k1/10;//2459/10=245//245/10=24//24/10=2//2/10=0

        }

        

        while(n>0)//2459>0 t//245>0 t

        {

            r=n%10;//2459%10=9//245%10=5

            if(r>min)//9>0 t//5>9 f

            {

                min=r;//variaable min to store the smallest digit//9

                c=c+1;//variable c to store the no of smallest digits //1

            }

            else 

            {

                break;

            }

            n=n/10;//2459/10=245

        }

        while(s>0)//9542>0 t//954>0 t//95>0 t//9>0 t//0>0 f

        {

            r=s%10;//9542%10=2//954%10=4//95%10=5//9%10=9

            if(r>max)//2>0 t//4>2 t//5>4 t//9>5 t

            {

                max=r;//variable max to store the largest no//2//4//5//9

                c1=c1+1;//variable c1 to store the no of largest digits//0+1=1//1+1=2//2+1=3//3+1=4

            }

            else

            {

                break;

            }

            s=s/10;//9542/10=954//954/10=95//95/10=9//9/10=0

        }

        if(c1==len)//4==4 t

        System.out.println("Increasing order");

        else if(c==len)

        System.out.println("Decreasing order");

        else 

        System.out.println("Bouncy no");

    }//main method closes

}//class closes


No comments:

Post a Comment

any problem in any program comment:-

Second largest number in java using ternary operator

 //Largest second number using ternary operator public class Main { public static void main(String[] args) { int a=5,b=6,c=7; int ...