Skip to content
Home » Python code to compute gcd of two numbers using recursion

Python code to compute gcd of two numbers using recursion

Program to compute greatest common divisor of two numbers.

def gcd(a,b):
    if (b==0):
        return a
    else:
        return gcd(b,a%b)
n1=int(input("Enter first number:"))
n2=int(input("Enter second number:"))
d=gcd(n1,n2)
print("GCD of",n1,"and",n2,"is:",d)

Output:

Enter first number:5
Enter second number:7
GCD of 5 and 7 is: 1