by CodeChum Admin
The greatest common divisor, also known as GCD, is the greatest number that will, without a remainder, fully divide a pair of integers.
Now, I want you to make a program that will accept two integers and with the use of loops, print out their GCD. Make good use of conditional statements as well.
Off you go!
N1=int(input("Enter the first number: "))
N2=int(input("Enter the second number: "))
def gcd(N1, N2):
if (N1 == 0):
return N2
if (N2 == 0):
return N1
if (N1 == N2):
return N1
if (N1 > N2):
return gcd(N1-N2, N2)
return gcd(N1, N2-N1)
if(gcd(N1, N2)):
print('GCD of', N1, 'and', N2, 'is', gcd(N1, N2))
else:
print('not found')
Comments
Leave a comment