SUNDAY, 12 JULY 2026
Guide For School logo Guide For SchoolStudy Guide For Students On Java Programming
Physics | Chemistry | Mathematics
ICSE | ISC | CBSE
Guide For School logo Guide For SchoolICSE and ISC Resources

Bouncy Number Program in Java and Python

14 February 2016

Bouncy Number program with algorithm, explanation, Java solution and simple Python solution for ICSE and ISC students.

Question:

Write a program 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. Example: 22344.

Decreasing Number: If no digit is exceeded by the digit to its right, it is called a decreasing number. Example: 774410.

Bouncy Number: A positive integer that is neither increasing nor decreasing is called a bouncy number. Example: 155349. There cannot be any bouncy numbers below 100.

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

Algorithm:

Step 1: Start.

Step 2: Accept a number from the user.

Step 3: Convert the number to a string so that adjacent digits can be compared.

Step 4: Check whether every digit is less than or equal to the next digit. If yes, the number is increasing.

Step 5: Check whether every digit is greater than or equal to the next digit. If yes, the number is decreasing.

Step 6: If the number is increasing, display that it is increasing and not bouncy.

Step 7: Otherwise, if the number is decreasing, display that it is decreasing and not bouncy.

Step 8: Otherwise, display that it is a Bouncy Number.

Step 9: Compare each digit with the next digit using a left-to-right index loop.

Step 10: Update the increasing flag when a digit is greater than the next digit.

Step 11: Update the decreasing flag when a digit is smaller than the next digit.

Step 12: Stop.

Explanation:

The program converts the number to a string because it needs to compare adjacent digits from left to right. This makes the comparison simple using charAt() in Java or indexing in Python.

The method isIncreasing() checks whether any digit is greater than the digit after it. If such a pair is found, the number cannot be increasing.

The method isDecreasing() checks whether any digit is smaller than the digit after it. If such a pair is found, the number cannot be decreasing.

The number is bouncy only when both checks fail. That means its digits neither remain in increasing order nor in decreasing order.

The program compares neighbouring digits from left to right. One flag records whether an increasing pattern is broken and another records whether a decreasing pattern is broken. A number is bouncy only when both ordered patterns fail.

A number is increasing if its digits never decrease from left to right, and decreasing if its digits never increase. A bouncy number is neither. The program therefore studies the order of adjacent digits. It may scan the number as a string or extract digits and compare neighbours. Two flags are useful: one to show that an increase was found and another to show that a decrease was found. If both situations occur, the number is bouncy. This logic is about digit trend, not digit value alone.

Java Program:

Java
/**
* The class BouncyNumber checks whether a number is increasing,
* decreasing or bouncy.
*
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
*/

import java.util.Scanner;

class BouncyNumber
{
    boolean isIncreasing(int n)
    {
        String s = Integer.toString(n);

        /*
        * If any digit is greater than the next digit,
        * the number is not increasing.
        */
        for(int i = 0; i < s.length() - 1; i++)
        {
            if(s.charAt(i) > s.charAt(i + 1))
            return false;
        }

        return true;
    }

    boolean isDecreasing(int n)
    {
        String s = Integer.toString(n);

        /*
        * If any digit is smaller than the next digit,
        * the number is not decreasing.
        */
        for(int i = 0; i < s.length() - 1; i++)
        {
            if(s.charAt(i) < s.charAt(i + 1))
            return false;
        }

        return true;
    }

    void checkBouncy(int n)
    {
        if(isIncreasing(n))
        System.out.println("The number " + n + " is Increasing and Not Bouncy");
        else if(isDecreasing(n))
        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.checkBouncy(n);
    }
}

Equivalent Python Program:

Python
# Read the number and keep any required copy for digit or divisor processing.
# Helper functions keep repeated calculations separate from the main logic.
# Loops and conditions implement the number-property test step by step.
# Display the result according to the flag/counter/calculated value.

def is_increasing(n):
    s = str(n)

    # If any digit is greater than the next digit, it is not increasing.
    for i in range(0, len(s) - 1):
        if s[i] > s[i + 1]:
            return False

    return True


def is_decreasing(n):
    s = str(n)

    # If any digit is smaller than the next digit, it is not decreasing.
    for i in range(0, len(s) - 1):
        if s[i] < s[i + 1]:
            return False

    return True


n = int(input("Enter a number: "))

if is_increasing(n):
    print("The number", n, "is Increasing and Not Bouncy")
elif is_decreasing(n):
    print("The number", n, "is Decreasing and Not Bouncy")
else:
    print("The number", n, "is Bouncy")

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

Leave a Reply

Your email address will not be published. Comments are reviewed before appearing publicly.

Send a comment or correction

Study smarter

Everything you need for ICSE and ISC Computer

Programs, revision notes, solved papers and practical guidance—organized for quick study.

Browse all resources →