I was just testing out my lowest common multiple (LCM) and highest common factor (HCF) functions but it returns the wrong value when I add the else statement.
Enter two numbers to find the lowest common multiple: 10 20
Lowest common multiple of 10 and 20 is: -1
Enter two numbers to find the highest common factor: 10 20
Highest common factor of 10 and 20 is: -1
Press any key to continue . . .
Without the else statement it works perfectly...
Enter two numbers to find the lowest common multiple: 10 20
Lowest common multiple of 10 and 20 is: 20
Enter two numbers to find the highest common factor: 10 20
Highest common factor of 10 and 20 is: 10
Press any key to continue . . .
The first iteration of the for loop (i = 1):
--->if ((1 % 10) == 0 && (1 % 20) == 0)
---> 1 % 10 = 1, thus the condition fails going to the else clause which returns -1
What you want to do is check every value of 1 <= i <= n1 * n2. Since the return statement is in the loop it is called on the first value of i that doesn't divide n1 or n2, thus you don't check every value of i in the range you are looking at.