Brijmohan lal Sahu - Facebook
ads
ads

Tuesday, November 4, 2014

Program to find L.C.M. of any two numbers in C++.

LCM of two numbers


Code Box
---------------------------------------

/*Program to find L.C.M. of any two numbers */

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int num1,num2,a,b,count,lcm,i;
lcm=1;

cout<<"\n----------------- L.C.M. Calculator ---------------";
cout<<"\nEnter first number:";
cin>>a;
cout<<"\nEnter second number:";
cin>>b;

num1=a;
num2=b;

//prime factors are 2,3,5,7
cout<<"\nL.C.M. Factors :";
for(i=7;i>=2;)
{
count=0;

if(num1%i==0)
{
num1=num1/i;
count=1;
}

if(num2%i==0)
{
num2=num2/i;
count=1;
}

if(count==0)
{
i--;
}
else
{
cout<<" "<<i;
lcm=lcm*i;
}
}


//in case of prime number
cout<<" "<<num1<<" "<<num2;
lcm=lcm*num1*num2;

cout<<"\nL.C.M. of "<<a<<" & "<<b<<" is :"<<lcm;
cout<<"\n\n-------------------------------------------";
getch();
}

No comments:

Post a Comment