Answer to Question #252566 in Python for Hazer

Question #252566

8. The GCD

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!


1
Expert's answer
2021-10-17T09:29:25-0400
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')

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS