Program opened below. You can scroll through the algorithm, Java code and output.
Program 08
Increasing, Decreasing or Bouncy Number
Question
Read a positive integer and classify its digits from left to right as nondecreasing, nonincreasing or bouncy.
Algorithm
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 program
Javaimport 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");
}
}
}Sample output
Input: 805349
Output: Bouncy