Program opened below. You can scroll through the algorithm, Java code and output.

Increasing, Decreasing or Bouncy Number

Download .java

Question

An increasing number has digits in non-decreasing order from left to right; a decreasing number has digits in non-increasing order. Accept a positive integer and classify it as increasing, decreasing or bouncy (neither).

Step 1: Start. Step 2: Declare the variables and data structures required for the task. Step 3: Input a positive integer N. Step 4: If N <= 0, display INVALID INPUT and stop. Step 5: Initialize the counters, flags, accumulators or result structures needed by the solution. Step 6: Convert N to a string. Step 7: Set inc=true and dec=true. Step 8: Compare each digit with the previous digit. Step 9: If a digit is smaller than its predecessor, set inc=false. Step 10: If a digit is greater than its predecessor, set dec=false. Step 11: If inc, display INCREASING; else if dec, display DECREASING; otherwise display BOUNCY. Step 12: Stop.
Java
import java.util.*;
class NumberTrend {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.next();
        if (!s.matches("[1-9][0-9]*")) {
            System.out.println("INVALID INPUT");
            return;
        }
    boolean inc = true, dec = true;
    for (int i = 1; i < s.length(); i++) {
        if (s.charAt(i) < s.charAt(i - 1)) {
            inc = false;
        }
    if (s.charAt(i) > s.charAt(i - 1)) {
        dec = false;
    }
}
if (inc) {
    System.out.println("INCREASING NUMBER");
}
else if (dec) {
    System.out.println("DECREASING NUMBER");
}
else {
    System.out.println("BOUNCY NUMBER");
}
}
}
Input: 1233 Output: INCREASING NUMBER
← Previous Program 71 of 84 Next →

Share a comment or request a program

Use this form to send a comment, report an issue, or suggest a new practical. Your message will be saved for review by the school.