Answers for "find lcm of list in python"

3

lcm in python program

#  Write a Program to Find LCM of two numbers entered by user

num1 = int(input("Enter first number: "))  
num2 = int(input("Enter second number: "))

if num1 > num2:
    max_num = num1
else:
    max_num = num2

while True :     
        if(max_num % num1 == 0 and max_num % num2 == 0):  
            print(" The LCM of ",num1," and ",num2," is ", max_num )  
            break 
        max_num = max_num+1
Posted by: Guest on October-27-2021
1

lcm of n numbers python

# importing the module
import math

# function to calculate LCM
def LCMofArray(a):
  lcm = a[0]
  for i in range(1,len(a)):
    lcm = lcm*a[i]//math.gcd(lcm, a[i])
  return lcm


# array of integers
arr1 = [1,2,3]
arr2 = [2,3,4]
arr3 = [3,4,5]
arr4 = [2,4,6,8]
arr5 = [8,4,12,40,26,28]

print("LCM of arr1 elements:", LCMofArray(arr1))
print("LCM of arr2 elements:", LCMofArray(arr2))
print("LCM of arr3 elements:", LCMofArray(arr3))
print("LCM of arr4 elements:", LCMofArray(arr4))
print("LCM of arr5 elements:", LCMofArray(arr5))
Posted by: Guest on December-11-2020
0

how to find lcm in python

def find_lcm(x, y):

   # choose the higher 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

num1 = 22 # You can input the numbers if u want
num2 = 56

# call the function
print("L.C.M :", find_lcm(num1, num2))
Posted by: Guest on June-25-2021

Python Answers by Framework

Browse Popular Code Answers by Language