Sunday , 15 October 2017
Home » Class 12 » Java Program to check for Bouncy Number, Increasing Number, Decreasing Number

Java Program to check for Bouncy Number, Increasing Number, Decreasing Number

Question:

Write a Program in Java to input a number and check whether it is a Bouncy Number or not.

Increasing Number : Working from left-to-right if no digit is exceeded by the digit to its left it is called an increasing number; for example, 22344.
Decreasing Number : Similarly if no digit is exceeded by the digit to its right it is called a decreasing number; for example, 774410.
Bouncy Number : We shall call a positive integer that is neither increasing nor decreasing a “bouncy” number; for example, 155349. Clearly there cannot be any bouncy numbers below 100.

Solution:

/**
* The class BouncyNumber inputs a number and checks whether it is a Bouncy Number or not
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
*/

import java.util.*;
class BouncyNumber
{
    boolean isIncreasing(int n) //Function to check whether a number is Increasing
    {
        String s = Integer.toString(n);
        char ch;
        int f = 0;
        for(int i=0; i<s.length()-1; i++)
        {
            ch = s.charAt(i);
            if(ch>s.charAt(i+1))// If any digit is more than next digit then we have to stop checking
            {
                f = 1;
                break;
            }
        }
        if(f==1)
            return false;
        else
            return true;
    }
    
    boolean isDecreasing(int n) //Function to check whether a number is Decreasing
    {
        String s = Integer.toString(n);
        char ch;
        int f = 0;
        for(int i=0; i<s.length()-1; i++)
        {
            ch = s.charAt(i);
            if(ch<s.charAt(i+1))// If any digit is less than next digit then we have to stop checking
            {
                f = 1;
                break;
            }
        }
        if(f==1)
            return false;
        else
            return true;
    }
    
    void isBouncy(int n)
    {
        if(isIncreasing(n)==true)
            System.out.println("The number " + n + " is Increasing and Not Bouncy");
        else if(isDecreasing(n)==true)
            System.out.println("The number " + n + " is Decreasing and Not Bouncy");
        else
            System.out.println("The number " + n + " is bouncy");
    }
    
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        BouncyNumber ob = new BouncyNumber();
        System.out.print("Enter a number : ");
        int n = sc.nextInt();
        ob.isBouncy(n);
    }
}

Output:

Enter a number : 22344
The number 22344 is Increasing and Not Bouncy
Enter a number : 774410
The number 774410 is Decreasing and Not Bouncy
Enter a number : 155349
The number 155349 is bouncy

Check Also

[Question 2] ISC 2017 Computer Practical Paper Solved – Quiz Result

Solution of Program 2 of ISC 2017 Computer Science Paper 2 (Practical) Exam. Java program to input the answers of each participant row-wise and calculate their marks

4 comments

  1. 155349
    1<5
    isIncreasing will return true…right?

    • No, as soon as it gets any digit more than earlier digit, it will return false. If all digits are less than next digit, then only it will return true.

  2. Will I get full marks if i perform string operations on number programs like kaprekar number or keith number?

Leave a Reply

Your email address will not be published. Required fields are marked *