Write a Python program to compute the greatest common divisor (GCD)


Introduction

Write a Python program to compute the greatest common divisor (GCD). I have used python 3.7 compiler for debugging purpose.


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

  1. def gcd(a, b):
  2. gcd = 1
  3.  
  4. if a % b == 0:
  5. return y
  6.  
  7. for k in range(int(b / 2), 0, -1):
  8. if a % k == 0 and b % k == 0:
  9. gcd = k
  10. break
  11. return gcd
  12.  
  13. print(gcd(20, 60))
  14. print(gcd(10, 40))

Result

python language techstudy.org

     





Related Topics




Popular Posts