Python Practice › Function Programs › Program 17
HCF function
Write a Python function to find the HCF of two numbers.
Input
24, 54
Output
6
Approach
- Euclid’s algorithm.
Solution
def hcf(a, b): while b: a, b = b, a % b return a print(hcf(24, 54))Output
6
Line by line
Line 3
(24, 54) → (54, 24) → (24, 6) → (6, 0), so the HCF is 6.
Learn the concept
New to this topic? Read the lesson, then come back.