I was trying to find the smallest common divisor for the numbers from 1 to 20.
I tried the following code. My problem was how to get back to the beggining of the for loop after the if test has found the number is not divisible.
But then i added the last statement i = 1; It seems to have worked because it gave me the write answer.
My question, how did the last statement tell the loop to restart again? Are there other technics of restarting a loop?
The code is used is below.
public class SmallDivisible {
static int numbi = 2520;
public static void main (String[] args){
for (int i = 1; i < 21; i++){
if (numbi % i == 0){
continue;
} else {
numbi++;
i = 1;
}
}
System.out.println(numbi);
}
}