Our website provided core java programs examples with output aid beginners and expert coders to test their knowledge gap and learn accordingly.
We’ll learn how to find the LCM of two numbers in Python programming in this tutorial.
LCM:
What exactly does LCM stand for? The Least Common Multiple of two numbers is referred to as LCM. The LCM of two numbers a and b, for example, is the lowest positive integer divisible by both.
Given two numbers a and b , the task is to write the program which calculates lcm of a and b in Python.
Examples:
Example1:
Input:
a= 23Â Â b=12
Output:
The LCM of the given two numbers 23 , 12 = 276
Example2:
Input:
a= 18 b=72
Output:
The LCM of the given two numbers 18 , 72 = 72
Example3:
Input:
a= 4 b=8
Output:
The LCM of the given two numbers 4 , 8 = 8
Example4:
Input:
a= 9 b=9
Output:
The LCM of the given two numbers 9 , 9 = 9
Explanation:
The least common divisor of two numbers is number itself if both the numbers are equal
Program to Calculate LCM of Two numbers in Python
There are several ways to calculate the lcm of two numbers in Python some of them are:
Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.
Method #1:Using while loop
Algorithm:
- Scan or give static input for the numbers a and b.
- Using an If condition, find the bigger value and assign it to the variable ‘max.’
- Check whether the remainder of (max %a) and (max %b) equals zero or not using an If condition within the while loop.
- Â Print max, which is the LCM of two numbers, if true.
- If not, use a break statement to skip that value.
- End of the execution of the program
Below is the implementation:
# given two numbers # given number a number1 = 23 # given number b number2 = 12 # finding bigger number of the given two numbers using if statement if(number1 > number2): maxValue = number1 else: maxValue = number2 while(True): if(maxValue % number1 == 0 and maxValue % number2 == 0): print("The LCM of the given two numbers", number1, ",", number2, "=", maxValue) break maxValue = maxValue + 1
Output:
The LCM of the given two numbers 23 , 12 = 276
Explanation:
- Both numbers must be entered by the user/static declaration and stored in separate variables.
- An if statement is used to determine which of the two values is smaller and save the result in a minimum variable.
- Then, unless break is used, a while loop is employed with a condition that is always true (or 1).
- Then, within the loop, an if statement is used to determine whether the value in the minimum variable is divisible by both numbers.
- If it is divisible, the break statement exits the loop.
- If it is not divisible, the minimum variable’s value is increased.
- The result(lcm ) will be printed.
Method #2:Using GCD/HCF of the given two numbers
The LCM of two numbers can be determined using the math module’s gcd() function. Consider the following illustration.
Below is the implementation:
# importing math import math # given two numbers # given number a number1 = 23 # given number b number2 = 12 # finding lcm of the given two values lcmValue = (number1*number2)//math.gcd(number1, number2) print("The LCM of the given two numbers", number1, ",", number2, "=", lcmValue)
Output:
The LCM of the given two numbers 23 , 12 = 276
Method #3:Using Numpy lcm function.
We can calculate the lcm of the given two numbers using numpy.
We use numpy lcm function to find the lcm of given two numbers.
First We need to import the numpy to use lcm function as below
import numpy as np
we can find lcm using np.lcm(number1,number2) .
Here number1 and number2 are the parameters of the lcm() function.
Below is the implementation:
# importing numpy import numpy as np # given two numbers # given number a number1 = 23 # given number b number2 = 12 # finding lcm of the given two numbers number1 and number2 lcmValue = np.lcm(number1, number2) # printing the lcm of number1 and number2 print("The LCM of the given two numbers", number1, ",", number2, "=", lcmValue)
Output:
The LCM of the given two numbers 23 , 12 = 276
Related Programs:
- python program to find the lcm of two numbers using recursion
- python program to find the product of two numbers using recursion
- python program to find the sum of natural numbers
- python program to find all numbers in a range which are perfect squares and sum of all digits in the number is less than 10
- python program to find the minimum difference between the index of two given elements present in an array
- java program to find lcm of two numbers
- python program to find the factorial of a number