Answer to Question #324584 in Java | JSP | JSF for Chiarra

Question #324584

Survival of the Biggest (JAVA)

by CodeChum Admin

Looping a number and taking away each digit of it is so much fun, but I wanted to try out a much more complex task: getting the largest digit among them all.

Think you can handle the job?


Instructions:

  1. Input a non-zero positive integer.
  2. Using the same concept as the previous problem, figure out how to separate the digits of a number and determine which of the digits is the largest one, using a while. Afterwards, print the largest digit.
  3. Tip #1: Create another variable that will hold the largest digit. Initial its value to a negative integer, like -1, outside the loop.
  4. Tip #2: Everytime you get the rightmost digit, check if it is greater than the current largest digit. If it is, set it as the new largest digit.

Input

A line containing an integer.

214

Output

A line containing an integer.

4
1
Expert's answer
2022-04-06T02:27:44-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