Sunday, 15 June 2014

C++ program to calculate Least Common Multiple(LCM)


Here is a C++ program to calculate Least Common Multiple(LCM)


 SOURCE CODE OUTPUT
#include<iostream>
using namespace std;
int main()
{
    int a2,b2,lcm=1,grtr;
    cout<<"Enter the first number ";
    cin>>a2;
    cout<<"Enter the second number ";
    cin>>b2;
    if(a2>b2)
        grtr=a2;
    else
        grtr=b2;
    for(int i=2;i<=grtr;i++)
    {
        while(a2%i==0||b2%i==0)
        {
            if(a2%i==0&&b2%i==0)
            {
                a2/=i;
                b2/=i;
            }
            else if(a2%i==0)
                a2=a2/i;
            else
                b2=b2/i;
            lcm*=i;
        }
    }
    cout<<"Lowest Common Multiple = "<<lcm;
    return 0;
}

Any questions regarding to program please write in comments.

No comments:

Post a Comment