Using Python!!! Write a recursive function gcd(m,n) that returns the greatest co
ID: 3668598 • Letter: U
Question
Using Python!!!
Write a recursive function gcd(m,n) that returns the greatest common divisor of a pair of numbers. The gcd of m and n is the largest number that divides both m and n. If one of the numbers is 0, then the gcd is the other number. If m is greater than or equal to n, then the gcd of m and n is the same as the gcd of n and m-n. If n is greater than m, then the gcd is the same as the gcd of m and n-m.
>>> gcd(5,0)
5
>>> gcd(15,5)
5
>>> gcd(5,7)
1
>>> gcd(24,144)
24
>>> gcd(124,144)
4
Explanation / Answer
# Python program to find the H.C.F of two input number
# define a function
def hcf(x, y):
"""This function takes two
integers and returns the H.C.F"""
if x==0:
return y
if y==0:
return x
# 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)):
hcf = i
return hcf
# take input from the user
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
print("The H.C.F. of", num1,"and", num2,"is", hcf(num1, num2))
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.