HCF function

EasyFunction Programs · 17 of 39

Write a Python function to find the HCF of two numbers.

Advertisement
Input
24, 54
Output
6

Approach

  1. Euclid’s algorithm.

Solution

Python
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.

Advertisement

Learn the concept

New to this topic? Read the lesson, then come back.

More function programs

← All 39 Function Programs