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:
Input a 3-digit integer.
Print the largest digit in the integer.
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.
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.
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.
import java.util.Scanner;
class Main {
public static void main(String[] args) {
//For example 3 digit integer number 256
//256%10=6 256/=10=25 25%10=5 (....)
System.out.print("Please enter:");
Scanner cin=new Scanner(System.in);
int threDigitIn=cin.nextInt();
//Native sole don't using loop or other
int one=threDigitIn%10;
threDigitIn/=10;
int two=threDigitIn%10;
threDigitIn/=10;
int thr=threDigitIn%10;
threDigitIn/=10;
System.out.println("Max Digit="+Math.max(Math.max(one,two),thr));
}
}
Comments
Leave a comment