Find the largest digit of a number in Java | Java Programming Solutions | Ask The Code
Problem:
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?
Input Sample:
214
Output Sample:
4
Code:
import java.util.Scanner;
class solution_Lar_Digit{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int max = 0;
while(n != 0){
int val = n % 10;
if (val > max) {
max = val;
}
n = n / 10;
}
System.out.println(max);
}
}
Comments