I've started to learn basics of Java and currently I have to find and print largest divisible number.
For example number 30 can be divided by 2, 3, 6 and 10 so on the console I should show only 10. Another example is number 12 which can be divided by 2, 3 and 6 - should show 6.
I'm stuck at the point where I show the largest number. What I have so far is this one
double isItDivisible = Double.parseDouble(scanner.nextLine());
int num = 2;
if ( isItDivisible % 2 == 0 ) {
System.out.println("The number is divisible by " + num);
} else if ( isItDivisible % 3 == 0 ) {
num = 3;
System.out.println("The number is divisible by " + num);
} else if ( isItDivisible % 6 == 0 ) {
num = 6;
System.out.println("The number is divisible by " + num);
} else if ( isItDivisible % 10 == 0 ) {
num = 10;
System.out.println("The number is divisible by " + num);
} else if ( isItDivisible % 7 == 0 ) {
System.out.println("The number is divisible by " + 7);
}
The snipped display the first occurrence instead of the largest. You can tell how new I'm based on the snipped so please bear with me.