Skip to content
Home » Greatest Common Divisor Python Program

Greatest Common Divisor Python Program

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

Greatest Common Divisor Python Program

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

Program:

# Python program to find GCD of two numbers

# inputs from user
x = int(input('Enter First Number: '))
y = int(input('Enter Second Number: '))

# find gcd of numbers
i = 1
while(i <= x and i <= y):
    if(x % i == 0 and y % i == 0):
        gcd = i
    i += 1

# print result
print('The GCD of',x,'and',y,'is',gcd)

Output:

Enter First Number: 16
Enter Second Number: 12
The GCD of 16 and 12 is 4

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

Similar Code : Python program to find GCD of two numbers