A Program to Calculate the LCM of Two Integers

In this example, we are giving a program to calculate the LCM(Lowest common multiple) of given two integers.


Program to calculate the LCM:

  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. int n1, n2, i, gcd, lcm;
  6. cout << "Enter two positive integers: ";
  7. cin >> n1 >> n2;
  8. for(i=1; i <= n1 && i <= n2; ++i)
  9. {
  10. if(n1%i==0 && n2%i==0)
  11. gcd = i;
  12. }
  13. lcm = (n1*n2)/gcd;
  14. cout << "LCM = " << lcm;
  15. return 0;
  16. }

Output:

Enter two positive integers: 12
15
LCM = 60