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:
Input
A line containing an integer.
214
Output
A line containing an integer.
4
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();
}
}
Comments
Leave a comment