- Related Questions & Answers
- Java program to find the LCM of two numbers
- Program to find LCM of two Fibonnaci Numbers in C++
- GCD and LCM of two numbers in Java
- Find GCD of two numbers
- C++ Program to Find the GCD and LCM of n Numbers
- LCM of an array of numbers in Java
- Calculating the LCM of multiple numbers in JavaScript
- Finding LCM of more than two (or array) numbers without using GCD in C++
- Find LCM of rational number in C++
- C++ Program to Find LCM
- Find max of two Rational numbers in C++
- Maximum sum of distinct numbers such that LCM of these numbers is N in C++
- How to Find LCM using Python?
- Maximum sum of distinct numbers with LCM as N in C++
- 8085 program to find larger of two 8 bit numbers

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

In mathematics Least Common Multiple (LCM) is the smallest possible integer, that is divisible by both numbers.

LCM can be calculated by many methods, like factorization, etc. but in this algorithm, we have multiplied the bigger number with 1, 2, 3…. n until we find a number which is divisible by the second number.

Input: Two numbers: 6 and 9 Output: The LCM is: 18

LCMofTwo(a, b)

**Input:** Two numbers a and b, considered a > b.

**Output: **LCM of a and b.

Begin lcm := a i := 2 while lcm mod b ≠ 0, do lcm := a * i i := i + 1 done return lcm End

#include<iostream> using namespace std; int findLCM(int a, int b) { //assume a is greater than b int lcm = a, i = 2; while(lcm % b != 0) { //try to find number which is multiple of b lcm = a*i; i++; } return lcm; //the lcm of a and b } int lcmOfTwo(int a, int b) { int lcm; if(a>b) //to send as first argument is greater than second lcm = findLCM(a,b); else lcm = findLCM(b,a); return lcm; } int main() { int a, b; cout << "Enter Two numbers to find LCM: "; cin >> a >> b; cout << "The LCM is: " << lcmOfTwo(a,b); }

Enter Two numbers to find LCM: 6 9 The LCM is: 18

Advertisements