Last Updated on
In this article, we will see how to find out LCM between 2 numbers programmatically. We have used Python language for finding LCM of 2 numbers.
What is LCM
LCM or Least Common Multiplier is a number which is smallest common multiplier of 2 or more numbers.
For example- LCM of 4 and 6 is 12. This mean 12 is smallest number which is multiple of both 4 and 6.
LCM of 10,25 and 40 is 200.
How to find LCM of numbers
To find LCM of2 (or more) numbers, we are going to first find factors of both numbers, multiply these factors while considering common factor as one.
Factor of 4= 1,2,4
Factor of 6= 1,2,3,6
As we can see product of 3 and 4 is multiple of both 4 and 6.
How to find LCM of 2 numbers by code
def getLCM(num1, num2): if(num1==num2): return num1 if(num1>num2): refnum=num1 else: refnum=num2 j=1 temp=refnum while(1): if(refnum%num1==0 and refnum%num2==0): return refnum break refnum=temp*(j+1) j+=1 print(getLCM(320,56))
In this article, we have seen how to find out LCM of 2 numbers by Python code. If in case you have doubt regarding implementation, please feel free to share us your concerns in discussion thread below, or mail us to email@example.com
This article is for technical interview preparation. Find more useful programming exercises in link below.
- How to check whether string is palindrome or not
- How to find prime numbers between a given range
- How to check whether number is Armstrong number
Xamnation is helping students in interview preparation and learning programming language. We are offering live classes, study material and interview preparation course. Please find below useful course.
- Aptitude preparation course for job interviews
- Puzzles course for job interviews
- Python course for job interviews
- Amazon job preparation course
- TCS job preparation course