Others have fixed your code already. If you would like a shorter approach you can look at this example:
public static void main(String[] args) {
int a = 60;
int b = 24;
for(int i = Math.min(a, b); i > 0; i--){
if(a % i == 0 && b % i == 0){
System.out.println("GCD: " + i);
break;
}
}
}
Output:
GCD: 12
By using Math.min()
you don't have to create two loops to see whether a
is where you should start, or b
.