Program opened below. You can scroll through the algorithm, Java code and output.
Program 71
Increasing, Decreasing or Bouncy Number
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).
Algorithm
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 program
Javaimport 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");
}
}
}Sample output
Input: 1233
Output:
INCREASING NUMBER