Python Program to Find HCF or GCD

Configurare noua (How To)

Situatie

The highest common factor (H.C.F) or greatest common divisor (G.C.D) of two numbers is the largest positive integer that perfectly divides the two given numbers. For example, the H.C.F of 12 and 14 is 2.

Backup

Using Loops

Input:

# define a function
def compute_hcf(x, y):

# choose the smaller number
    if x > y:
        smaller = y
    else:
        smaller = x
    for i in range(1, smaller+1):
        if((x % i == 0) and (y % i == 0)):
            hcf = i 
    return hcf

num1 = 54 
num2 = 24

print("The H.C.F. is", compute_hcf(num1, num2))

Output:
The H.C.F. is 6

Solutie

Tip solutie

Permanent

Voteaza

(10 din 14 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?