Loop up to the smaller number, keep the largest counter that divides both, then get the LCM from one formula
The HCF and LCM of two numbers is a classic beginner problem and a common interview question. In this lesson we find the HCF (highest common factor) with one simple loop, draw the flowchart, and then get the LCM from a one-line formula.
Our example: 12 and 18.
What are the HCF and LCM of two numbers?
The HCF (highest common factor, also called the GCD) is the largest number that divides both numbers exactly. The LCM (least common multiple) is the smallest number that both numbers divide into.
For 12 and 18: the common factors are 1, 2, 3 and 6, so the HCF is 6. The LCM is 36.
Step 1: find the common factors by hand
| Counter | 12 % counter | 18 % counter | Divides both? | |—|—|—|—| | 1 | 0 | 0 | yes | | 2 | 0 | 0 | yes | | 3 | 0 | 0 | yes | | 4 | 0 | 2 | no | | 5 | 2 | 3 | no | | 6 | 0 | 0 | yes | | 7 to 12 | | | no |

The largest number that divides both is 6. That is the HCF.
Step 2: why the loop stops at the smaller number
A number bigger than 12 cannot divide 12, so it cannot be a common factor of 12 and 18. So the loop counter (LC) only needs to run from 1 up to the smaller number. In the flowchart we first find the smaller number and store it in a variable called small.
Step 3: the test, both remainders zero
On every step we check whether LC divides both numbers:
n1 % LC == 0 AND n2 % LC == 0
The number goes on the left of %. (On the board in the video this was first written the other way round; the video shows the corrected version.) Whenever the test is true, we store HCF = LC. Because LC keeps growing, the last value stored is the largest common factor.
Step 4: the HCF flowchart
- Start, read n1 and n2, set HCF = 1.
- n1 < n2? Yes: small = n1. No: small = n2.
- LC = 1.
- LC <= small? If no, the loop is over: go to step 7.
- n1 % LC == 0 AND n2 % LC == 0? Yes: HCF = LC.
- LC = LC + 1, and back to step 4.
- LCM = n1 x n2 / HCF, print HCF and LCM, End.

Step 5: the LCM from the HCF
For two positive whole numbers, the product of the numbers equals the product of their HCF and LCM. So:
LCM = (n1 x n2) / HCF
= (12 x 18) / 6
= 216 / 6 = 36
From flowchart to code
small = n1 if n1 < n2 else n2
hcf = 1
for LC = 1 to small
if n1 % LC == 0 and n2 % LC == 0
hcf = LC
lcm = n1 * n2 / hcf
print hcf, lcm
Later you will meet Euclid’s algorithm, which finds the HCF in far fewer steps; the loop above is the clearest way to see what the HCF is.
Video chapters
- 0:00 HCF and LCM: the problem
- 0:30 HCF of 12 and 18 on the board
- 2:18 Common factors: 1, 2, 3, 6
- 3:10 The logic: loop to the smaller number
- 3:38 The test: both remainders zero
- 4:43 Drawing the flowchart
- 7:34 LCM = a x b / HCF
- 8:42 Wrap-up
Watch the short versions
Short on time? These Shorts from this lesson each make one point in under a minute.
- HCF Trick: Loop Only to the Smaller Number (20 s Short, coming soon, from 2:00 in the full video)
- HCF Test: Both Remainders Must Be Zero (37 s Short, coming soon, from 3:37 in the full video)
- LCM in One Line: a x b / HCF (45 s Short, coming soon, from 7:47 in the full video)
Key takeaways
- The HCF is the largest number that divides both numbers; for 12 and 18 it is 6.
- A common factor can never be bigger than the smaller number, so the loop stops there.
- Test both remainders: n1 % LC == 0 AND n2 % LC == 0, and keep the last LC that passes.
- LCM = a x b / HCF; for 12 and 18 that is 36.
Frequently asked questions
Why does the loop start at 1?
1 divides every number, so HCF is at least 1. Starting at 1 also handles two numbers that share no other factor, like 7 and 13.
Is HCF the same as GCD?
Yes. Highest common factor (HCF) and greatest common divisor (GCD) are two names for the same number.
Why is LCM = a x b / HCF?
For two positive whole numbers, a x b = HCF x LCM. Dividing both sides by the HCF gives the formula.
Further reading
- Java program to find HCF and LCM of two numbers (BeginnersBook): the same HCF-then-LCM idea in code
- Python: find HCF and LCM of two numbers (PYnative): loop and Euclid versions
Practice: find the HCF and LCM of 24 and 36 with this flowchart. How many times does the loop run?
Next in the series: what a variable is and how it lives in memory. Subscribe and follow along.
