m2skills

gcdEuclid python

May 23rd, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.89 KB | None | 0 0
  1. # program to find gcd of 2 numbers
  2.  
  3. # method to find gcd of 2 numbers
  4. # keep subtracting smaller number from larger until they both are equal
  5. def gcd(num1,num2):
  6.     if num1 > num2:
  7.         while num2 != 0:
  8.             # calculate the quotient
  9.             quotient = int(num1 / num2)
  10.             # store the num2 in temp
  11.             temp = num2
  12.             num2=num1-x*num2
  13.             num1=temp
  14.         return num1
  15.     else:
  16.         while num1 != 0:
  17.             # calculate the quotient
  18.             quotient = int(num2/num1)
  19.             # store the num1 in temp
  20.             temp=num1
  21.             num1=num2-x*num1
  22.             num2=temp
  23.         return num2
  24.  
  25.  
  26. # main function
  27. cont = True
  28. while cont:
  29.  
  30.     number1 = int(input("Enter the first number : "))
  31.     number2 = int(input("Enter the second number : "))
  32.     answer = gcd(number1, number2)
  33.     print("The GCD of " + str(number1) + " and " + str(number2) + " is " + str(answer))
  34.  
  35.     n = int(input("Do you want to continue (1/0): "))
  36.     if n == 0:
  37.         cont = False
  38.         print()
Add Comment
Please, Sign In to add comment