Answer to Question #266282 in Java | JSP | JSF for Madison

Question #266282

Write a program in java to take a number as input then display the highest digit and lowest digit present in it. Use method prototype: void check(int number). The program must use 3-digit and 4-digit numbers and 1-digit and 2-digit numbers are not to be accepted.


1
Expert's answer
2021-11-15T07:38:35-0500
public class Solution {
  public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    int a = s.nextInt();
    if (a < 100) {
      System.out.println("Please enter more than two digit number!");
    } else {
      int min = 9;
      int max = 0;
      int digit;
      while (true) {
        digit = s % 10;
        if (min > digit) min = digit;
        if (max < digit) max = digit;
        s /= 10;
        if (s == 0) break;
      }
      System.out.println("Min digit: " + min + "\n" +
                         "Max digit: " + max);
    }
  }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS