write a program in java to input a number and print the largest digit from the number
Answers
Hi
How can I get the maximum digit from a number entered by the user?
Example: if user inputs: 1521, the program should output 5 as maximum.
Any help please?
NOTE: I want it to be made using 2 methods. AND don't give me answers with arrays. :)
This is what I wrote so far..
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int number = 0;
System.out.println("Enter a number: ");
number = input.nextInt();
System.out.print("Max is: "+max(number));
}
public static int maximum(int max){
int num = 0;
while(num != 0){
int rightDigit = num % 10;
num /= 10;
if(rightDigit > max)
rightDigit = max;
}
return max;
}
When I run it, whatever number I write.. I get Max: 0.