Skip to content
Home » Python program to find GCD of two numbers

Python program to find GCD of two numbers

Learn about the Python program to find the GCD of two numbers in the below code example. Also, refer to the comments in the code snippet to get a detailed view about what’s actually happening.

Python program to find GCD of two numbers

The below program finds the GCD of the two numbers given by the user and prints the output.

Program:

# Python program to find GCD of two numbers

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

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

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

Output:

Enter First Number: 10
Enter Second Number: 8
The GCD of 10 and 8 is 2

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

Similar Code : LCM of Two Numbers in Python using Recursion