Back to Tutorials
TutorialsJava

An easy Bouncy Number in java

In an increasing integer the first digit will be smaller than rest of the digits. For example, 1234 and 14779 are both increasing integers. In an decreasing integer the first digit will be bigger than rest of the digits. For example, 4321 and 9731 are both decreasing integers.

Trushna Tejwani22 February 2021
Tutorials

Watch java videos

A bouncy number is any positive integer that is neither increasing nor decreasing.

In an increasing integer the first digit will be smaller than rest of the digits. For example, 1234 and 14779 are both increasing integers. In an decreasing integer the first digit will be bigger than rest of the digits. For example, 4321 and 9731 are both decreasing integers.

import java.util.*;
class Bouncy
{
boolean isAscending(int num)
    {
        String str=String.valueOf(num);
        int j=0;
        for(int i=1;i<str.length();i++)
        {
            int n=Integer.valueOf(str.charAt(j));
            int m=Integer.valueOf(str.charAt(i));
            if(m<n)
                 return false;
        }
        return true;
    }

    boolean isDescending(int num)
    {
        String str=String.valueOf(num);
        int j=0;
        for(int i=1;i<str.length();i++)
        {
            int n=Integer.valueOf(str.charAt(j));
            j++;
            int m=Integer.valueOf(str.charAt(i));
            if(m>n)
                return false;
        }
        return true;
    }

    boolean isBouncy(int num)
    {
        if(num>0)
        {
            boolean a=isAscending(num);
            boolean b=isDescending(num);
            if(a==false && b==false)
                return true;
            else 
                return false;
        }
        else
            return false;
    }

    void PrintBouncy(int a,int b)
    {
        boolean c;
        for(int i=a;i<=b;i++)
        {
            c=isBouncy(i);
            if(c==true)
                System.out.print(i+"  ");
        }
    }
    
    public static void main()
    {
        Scanner sc=new Scanner(System.in);
        Bouncy obj=new Bouncy();
        System.out.println("Enter the range to print the Bouncy numbers");
        int p=sc.nextInt();
        int q=sc.nextInt();
        obj.PrintBouncy(p,q);
    }
}

GoldBach Number