2
def calculate_lcm(x, y):
    if x > y:
        greater = x
    else:
        greater = y
    while (True):
        if ((greater == x == 0) and (greater == y == 0)):
            lcm = greater
            break
        greater += 1


num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
# printing the result for the users  
print("The L.C.M. of", num1, "and", num2, "is", calculate_lcm(num1, num2))

I am getting error please help me.

S.B
  • 13,077
  • 10
  • 22
  • 49
  • What is the error? – matszwecja Feb 24 '22 at 12:06
  • he have used == sign where he have to use % sign according to me – Quad gamer Feb 24 '22 at 12:08
  • 2
    Not related to the question but in many cases, finding HCF and using the `a*b=LCM*HCF` formula would be better. This is because of the LCM is much larger than the numbers, It will take a lot of time. Also, Try this out [Built-in module to calculate the least common multiple](https://stackoverflow.com/questions/51716916/built-in-module-to-calculate-the-least-common-multiple) – Anshumaan Mishra Feb 24 '22 at 12:16
  • 1
    I think this question should be closed as a duplicate. – Abhyuday Vaish Feb 24 '22 at 12:57

3 Answers3

2
# defining a function to calculate LCM  
def calculate_lcm(x, y):  
    # selecting the greater number  
    if x > y:  
        greater = x  
    else:  
        greater = y  
    while(True):  
        if((greater % x == 0) and (greater % y == 0)):  
            lcm = greater  
            break  
        greater += 1  
    return lcm    
  
# taking input from users  
num1 = int(input("Enter first number: "))  
num2 = int(input("Enter second number: "))  
# printing the result for the users  
print("The L.C.M. of", num1,"and", num2,"is", calculate_lcm(num1, num2)) 

Not sure but it might help and this question is repeated many times.

S.B
  • 13,077
  • 10
  • 22
  • 49
Quad gamer
  • 85
  • 6
2

Please Try My Code and If Get Any Error Then Comment

# defining a function to calculate LCM  
def calculate_lcm(x, y):  
    l = [x,y]
    lesser = min(l)
    greater = max(l)
    if greater % lesser ==0:
        return(greater)
    else:
        i = 1
        while True:
            if (greater*i)%lesser == 0:
                return(greater*i)
            elif i > 100:
                return(greater*lesser)
            i+=1
      
  
# taking input from users  
num1 = int(input("Enter first number: "))  
num2 = int(input("Enter second number: "))  
# printing the result for the users  
print("The L.C.M. of", num1,"and", num2,"is", calculate_lcm(num1, num2)) 
codester_09
  • 5,622
  • 2
  • 5
  • 27
2

Here is another approach using GCD by implementing Euclidean algorithm.

# Function for calculating GCD 
def calc_gcd(x, y):

   while(y):
       x, y = y, x % y
   return x

# Function for calculating LCM 
def calc_lcm(x, y):
   lcm = (x*y)//calc_gcd(x,y)
   return lcm

# taking input from users  
num1 = int(input("Enter first number: "))  
num2 = int(input("Enter second number: "))  
# printing the result for the users  
print("The L.C.M. of", num1,"and", num2,"is", calc_lcm(num1, num2)) 
Muhammad Mohsin Khan
  • 1,444
  • 7
  • 16
  • 23