C++ Program to Calculate LCM of Two Numbers using While Loop

Write C++ Program to Calculate LCM of Two Numbers using While Loop

// CPP Program to Calculate LCM of Two Numbers using While Loop

/* LCM is the smallest number which is divided by both */

#include <iostream>

using namespace std;

int main()
{
	int n1, n2, m, n;

	cout << "Enter number 1 :--> ";
	cin >> n1;

	cout << "Enter number 2 :--> ";
	cin >> n2;

	m = n1;
	n = n2;

	int number1 = n1, number2 = n2;

	while(n1 != n2)
	{
		if(n1 > n2)
		{
			n1 = n1 - n2;
		}
		else
		{
			n2 = n2 - n1;
		}

	}

	int gcd = n1;

	cout << "LCM of " << m << " and " << n <<
            " is : " << (number1 * number2) / gcd;

	return 0;
}

Output:

Enter number 1 :--> 25
Enter number 2 :--> 12

LCM of 25 and 12 is : 300

≪ Previous | Next ≫