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

Question #330249

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:



Input a 3-digit integer.


Print the largest digit in the integer.


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.


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.


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-20T03:44:58-0400



import java.util.Scanner;


class Main {
  public static void main(String[] args) {
     //For example 3 digit integer number 256
    //256%10=6 256/=10=25 25%10=5 (....)
    System.out.print("Please enter:");
    Scanner cin=new Scanner(System.in);
    int threDigitIn=cin.nextInt();
    //Native sole don't using loop or other
    int one=threDigitIn%10;
    threDigitIn/=10;
    int two=threDigitIn%10;
    threDigitIn/=10;
    int thr=threDigitIn%10;
    threDigitIn/=10;
    System.out.println("Max Digit="+Math.max(Math.max(one,two),thr));
  }
}

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