C++ Program to find LCM

Last Updated:

What is Least Common Multiple?

A smallest common multiple of two or more integers.

In general, Least Common Multiple (LCM) is otherwise called as Lowest Common Multiple (LCM) or Smallest Common Multiple (SCM)

Tips: It is recommended to use our online LCM calculator for better understanding.

Examples

The following table provides few examples of LCM of the given numbers.

NumbersLCM
4, 10 20
6, 5 30
15, 30 , 45 90

LCM of Two Numbers

n the following example, we will find the LCM of the given two numbers (45, 90).

Example

C++ Compiler
#include <iostream> using namespace std; int main() { int num1 = 45; int num2 = 90; int max; max = (num1 > num2) ? num1 : num2; // Always true while(1) { if((max % num1 == 0) && (max % num2 == 0)) { cout << "LCM of " << num1 << " and " << num2 << ": " << max; break; } ++max; } return 0; }

LCM of any Two Given Numbers

In the following example, we will find the LCM of any two given numbers.

Example

C++ Compiler
#include <iostream> using namespace std; int main() { int num1, num2, max; cout << "Enter positive (int) num1 = "; cin >> num1; cout << "Enter positive (int) num2 = "; cin >> num2; max = (num1 > num2) ? num1 : num2; // Always true while(1) { if((max % num1 == 0) && (max % num2 == 0)) { cout << "\nLCM of " << num1 << " and " << num2 << ": " << max; break; } ++max; } return 0; }

Share this Page

Meet the Author