Robotics & Artificial Intelligence

Write a user-defined function def HCF() to input two numbers from the user. The program calculates greatest common divisor of the two numbers inside the function. Finally, it returns the value to main program to display the result.

Sample Input: First Number: 25

Second Number: 60

HCF = 5

Python Functions

1 Like

Answer

def HCF(a, b):

    if(a < b):
        small = a
    else:
        small = b

    for i in range(1, small + 1):
        if(a % i == 0 and b % i == 0):
            hcf = i
    return(hcf)

# main program
num1 = int(input("Enter first number:"))
num2 = int(input("Enter second number:"))
result = HCF(num1, num2)
print("HCF =", result)

Output

Enter first number:25
Enter second number:60
HCF = 5

Answered By

1 Like


Related Questions