A Program to Find GCD

In this example, we are giving a program to find GCD(greatest common divisor).


Program to find GCD:

  1. # Find GCD
  2. def compute_gcd(x, y):
  3. if x > y:
  4. smaller = y
  5. else:
  6. smaller = x
  7. for i in range(1, smaller+1):
  8. if ((x % i == 0) and (y % i == 0)):
  9. gcd = i
  10. return gcd
  11. num1 = int(input('Enter 1st number: '))
  12. num2 = int(input('Enter 2nd number: '))
  13. print("The G.C.D. is", compute_gcd(num1, num2))

Output:

Enter 1st number: 65
Enter 2nd number: 35
The G.C.D. is 5