Answer to Question #314979 in Java | JSP | JSF for Inaya Ishaan

Question #314979

Largest Digit

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.

Instructions

  1. Input one 3-digit integer.
  2. Print the largest digit in the integer. (Hint: use % 10 to get the rightmost digit and / 10 to remove it)

Input

A line containing a three-digit integer.

173

Output

A line containing a single-digit integer

7


"The code should use If-Else-Elseif statement"


1
Expert's answer
2022-03-21T06:49:53-0400


import java.util.Scanner;


public class App {
	
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int number = in.nextInt();
		int largestDigit = 0;
		while (number > 0) {
			int digit = number % 10;
			if (largestDigit < digit) {
				largestDigit = digit;
			}
			number = number / 10;
		}
		System.out.println(largestDigit);
		in.close();
	}


}

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