Skip to content
Home » GCD Program in Python using Function

GCD Program in Python using Function

Learn about GCD Program in Python using Function in the below code example. Also, refer to the comments in the code snippet to get a detailed view about what’s actually happening.

GCD Program in Python using Function

The Highest Common Factor (HCF), also known as gcd, can be computed in Python using a single function provided by the math module, making tasks easier in a variety of situations.

Program:

# Python program to find GCD of two numbers using function

#user-defined function
def compute_gcd(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)):
            gcd = i 
    return gcd

# inputs from user
num1 = int(input('Enter First Number: '))
num2 = int(input('Enter Second Number: '))

# function call
print('The GCD of',num1,'and',num2,'is',compute_gcd(num1, num2))

Output:

Enter First Number: 72
Enter Second Number: 36
The GCD of 72 and 36 is 36

Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.

Similar Code : Greatest Common Divisor Python Program