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

Increasing, Decreasing or Bouncy Number

Download .java

Question

Read a positive integer and classify its digits from left to right as nondecreasing, nonincreasing or bouncy.

Step 1: Start the program. Step 2: Read a positive integer as digit characters. Step 3: Assume initially that the digits are increasing. Step 4: Assume initially that the digits are decreasing. Step 5: Set the comparison position to the second digit. Step 6: Compare this digit with its immediately previous digit. Step 7: If the current digit is smaller, clear the increasing flag. Step 8: If the current digit is larger, clear the decreasing flag. Step 9: Move one position to the right. Step 10: Continue until all adjacent digit pairs are compared. Step 11: If the increasing flag remains true, report Increasing. Step 12: Otherwise, if the decreasing flag remains true, report Decreasing. Step 13: If neither flag remains true, report Bouncy. Step 14: Stop.
Java
import java.util.*;
class Bouncy {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.next();
        boolean rise = true;
        boolean fall = true;
        for (int i = 1; i < s.length(); i++) {
            if (s.charAt(i) < s.charAt(i - 1)) {
                rise = false;
            }
            if (s.charAt(i) > s.charAt(i - 1)) {
                fall = false;
            }
        }
        if (rise) {
            System.out.println("Increasing");
        } else if (fall) {
            System.out.println("Decreasing");
        } else {
            System.out.println("Bouncy");
        }
    }
}
Input: 805349 Output: Bouncy
← Previous Program 8 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.