Return to the BobbyGriggs.com C++ Nook


Chapter 6 - Least Common Multiple
© 1998 by Bobby Griggs.



// Program name:  LCM

// Purpose:  To compute the least common multiple of two integers.



// Win32 console application



#pragma hdrstop



#include 

#include "myprocs.h"



void Input_integers(int &a,int &b);

void Calculate_lcm(int a,int b,int &l);

void Display_lcm(int a,int b,int l);



void main()

{

	int num1,num2,lcm;

	bool doagain = true;



	while (doagain)

	{

		Input_integers(num1,num2);

		Calculate_lcm(num1,num2,lcm);

		Display_lcm(num1,num2,lcm);

		doagain = Again();

	}

	return;

}



void Input_integers(int &a,int &b)

{

	cout << "Compute LCM of two integers!" << endl << endl;

	cout << "Enter first integer ... ";

	cin >> a;

	cout << endl;

	cout << "Enter second integer ... ";

	cin >> b;

	cout << endl << endl;

	Swap(a,b);

	return;

}



void Calculate_lcm(int a,int b,int &l)

{

	int i;



	if (b % a == 0)

	{

		l = b;

		return;

	}

	else

	{

		for (i = b;i <= a * b;i++)

			if ((i % b == 0) && (i % a == 0))

			{

				l = i;

				return;

			}

	}

}



void Display_lcm(int a,int b,int l)

{

	cout << "The LCM of " << a << " and " << b << " is ";

	cout << l << "!" << endl << endl;

	return;

}