Tuesday, July 14, 2009

Can anyone give me an algorithm to find L.C.M. of two nos. without finding G.C.D...?

can u tell why this program didn't run???


#include%26lt;stdio.h%26gt;


#include%26lt;conio.h%26gt;








void main()


{


int a,b,m1,m2,i,j,c,lcm;


printf("\n Enter 2 numbers ");


scanf("%d%d",%26amp;a,%26amp;b);





i=2;


c=0;


j=2;


m1=a;


m2=b;


lcm=b;


while(c==0)


{ while(m1%26lt;=m2)


{ if(m1==m2)


{lcm=m1;


c=1;


}


else


{m1=a*i;


i++;


}


}


m2=b*j;


j++;


}


printf("lcm=%d",lcm);





getch();


}

Can anyone give me an algorithm to find L.C.M. of two nos. without finding G.C.D...?
Your algorithm is 1) incomplete and 2) enters an endless loop





Try this one





The function computes the product (always a common multiple) and systematically removes common factors:





public void function lcm(int a, int b)


{


int c,d;





if (a %26gt; b) { c = b ;} else { c = a; }


c = sqrt(c)++;


while (((a % 2) = 0) %26amp;%26amp; ((b % 2) = 0))


{


d = d / 2;


a = a / 2;


b = b / 2;


}


i = 3;


while (i %26lt;= c)


{


while (((a % i) = 0) %26amp;%26amp; ((b % i) = 0))


{


d = d / i;


a = a / i;


b = b / i;


}


}


i = i + 2;


}


return (d);


}
Reply:This appears to be Java. There are several Java development and debugger programs available.


Do you get an error code? Can you verify that the variables have the correct value at intermediate steps?





http://www.debugtools.com/


No comments:

Post a Comment