Write a program that defines a function to find the GCD of two numbers using the algorithm below. The greatest common divisor (GCD) of two values can be computed using Euclid's algorithm. Starting with the values m and n, we repeatedly apply the formula: n, m = m, n%m until m is 0. At that point, n is the GCD of the original m and n (Use Recursion) in python
Code
def gcd(a, b):
if b == 0:
return a
else:
return gcd(b, a % b)
if __name__ == "__main__":
a = int(input("Enter a number: "))
b = int(input("Enter another number: "))
print("The greatest common divisor of", a, "and", b, "is", gcd(a, b))
Output
0 Comments
Post a Comment