Python Program to Find HCF or GCD

In this blog post , we are going to learn how to find the H.C.F of two numbers using function and for loops.


Program to Find HCF or GCD in Python

# Python program to find the H.C.F or GCD of two input number

# defining a function to calculate H.C.F
def calculateHCF(number1, number2):

# Below we are choosing the smaller number
    if number1 > number2:
        smaller_num = number2
    else:
        smaller_num = number1
    for k in range(1, smaller_num+1):
        if((number1 % k == 0) and (number2 % k == 0)):
            num_hcf = k
            
    return num_hcf



#taking input from the user
number1 = int(input("Please Enter first number: "))
number2 = int(input("Please Enter second number: "))

print("The H.C.F. of", number1,"and", number2,"is", calculateHCF(number1, number2))

Output:-

Please Enter first number: 66
Please Enter second number: 12
The H.C.F. of 66 and 12 is 6

 


 

https://bit.ly/2lgLuJ1 https://bit.ly/2lAbgYW https://bit.ly/2ngU8ry

Leave a Reply

Your email address will not be published. Required fields are marked *