Answer to Question #330250 in Java | JSP | JSF for maica

Question #330250

This one is a bit tricky. You're going to have to isolate each digit of the integer to determine which one is the largest, so good luck!


Instructions:

  1. Input a 3-digit integer.
  2. Print the largest digit in the integer.
  3. Tip #1: Use % 10 to get the rightmost digit. For example, if you do 412 % 10, then the result would be the rightmost digit, which is 2.
  4. Tip #2: On the other hand, use / 10 to remove the rightmost digit. For example, if you do 412 / 10, then the result would be 41.
  5. Tip #3: You'd have to repeat Tip #1 and Tip #2 three times for this problem because the input is a 3-digit integer.
1
Expert's answer
2022-04-20T12:40:59-0400



import java.util.Scanner;
//Implement class Cube
class Main {
  
  public static void main(String[] args) {
    System.out.print("Please enter three digits number: ");
    Scanner cin=new Scanner(System.in);
    int a=cin.nextInt();
    int mxDigit=0;//Max digit save here
    while(a!=0)
      {
        mxDigit=Math.max(mxDigit,a%10);
        a/=10;
      }
    System.out.println("Max Digit for given number: "+mxDigit);
  }
}

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