Pages

Wednesday, November 11, 2015

C++ Program to Compute The Least Common Multiple of Integers

C++ program to compute the LCM of two numbers. Least Common Multiple can be gotten by dividing the numbers by their common factors.Using the logic below, we can also find write a program to calculate the LCM for more numbers.

//C++ program that computes the least common multiple of two integers. These are represented as a and b below.

#include<iostream.h>
#include<conio.h>
void main()
{
int a,b,i,j; //Variable declarations
clrscr;
cout<<"enter a number: ";
cin>>a;
cout<<"enter a number: ";
cin>>b;
i=a ;
j=b;
if(a==0||b==0) //Checks if either a OR b is 0
cout<<" The LCM is 0"; //If 0 from the condition above, a display message is output to the screen
else
{
while(a!=b)//While the value of a is not equal to b execute the statements below
{
if(a>b)
b=b+j;
else
if(a<b)
a=a+i;
}
cout<<"LCM:"<<" "<<a;
}
}