m2skills

LCM PYTHON

Jul 26th, 2017
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.03 KB | None | 0 0
  1. # Program to find LCM of 2 given numbers
  2.  
  3. # method to calculate gcd of 2 numbers
  4. def gcd(num1,num2):
  5.     if num1 == 1 or num2 == 1:
  6.         return 1
  7.     if abs(num1 - num2) == 1:
  8.         return 1
  9.     while num1 != num2:
  10.         if num1>num2:
  11.             num1 -= num2
  12.         else:
  13.             num2 -= num1
  14.     return num1
  15.    
  16. # method to calculate LCM of 2 numbers
  17. def LCM(num1, num2):
  18.    
  19.     gcd_num = gcd(num1, num2)
  20.     lcm = (num1 * num2) / gcd_num
  21.     return lcm
  22.    
  23.  
  24. # main function
  25. cont = True
  26. while cont:
  27.    
  28.     number1 = int(input("\nEnter the first number : "))
  29.     number2 = int(input("Enter the second number : "))
  30.     answer = LCM(number1, number2)
  31.     print("The LCM of " + str(number1) + " and " + str(number2) + " is " + str(answer))
  32.    
  33.     n = int(input("Do you want to continue (1/0): "))
  34.     if n == 0:
  35.         cont = False
  36.         print()
  37.    
  38. '''
  39. Enter the first number : 14
  40. Enter the second number : 16
  41. The LCM of 14 and 16 is 112.0
  42. Do you want to continue (1/0): 1
  43.  
  44. Enter the first number : 15
  45. Enter the second number : 25
  46. The LCM of 15 and 25 is 75.0
  47. Do you want to continue (1/0): 0
  48.  
  49. '''
Add Comment
Please, Sign In to add comment