C++ Program to Find LCM of Two Numbers using For loop
ADVERTISEMENTS
C++ program to find lcm of two numbers using for loop. In this article, you will learn how to find lcm of two numbers in c++ language using for loop.
C++ Program to Find LCM of Two Numbers using For loop
// C++ Program to Find LCM of Two Numbers using For loop
#include <iostream>
using namespace std;
int main() {
int p, q, i, g, x;
// p & q - To store the two positive numbers
// x - To store the LCM number
// g - To store the GCD
cout << "-----Enter the two positive integer numbers-----\n";
cin >> p >> q;
for (i = 1; i <= p && i <= q; ++i) {
if (p % i == 0 && q % i == 0) {
g = i;
}
}
x = (p * q) / g;
cout << "\nThe LCM of two positive numbers " << p << " & " << q << " is " << x << ".\n";
return 0;
}
Output
-----Enter the two positive integer numbers-----
110
160
The LCM of two positive numbers 110 & 160 is 1760.
C++ Program to Find LCM of Two Numbers using While loop
// C++ Program to Find LCM of Two Numbers using While loop
#include <iostream>
using namespace std;
int main() {
int p, q, x;
// p & q - To store the two positive numbers
// x - To store the LCM number
cout << "-----Enter the two positive integer numbers-----\n";
cin >> p >> q;
x = p > q ? p : q;
while (1) {
if (x % p == 0 && x % q == 0) {
cout << "\nThe LCM of the " << p << " and " << q << " numbers is " << x << ".\n";
break;
}
++x;
}
return 0;
}
Output
-----Enter the two positive integer numbers-----
110
150
The LCM of the 110 and 150 numbers is 1650.