Finding LCM & GCD in C
Two of my friends faced a problem with writing a program which finds the LCM(Lowest Common Multiple)/GCD(Greatest Common Divisor) of two positive integers, so I helped them out by writing two functions for each. I thought many others might be having the same problem.
So here are the functions: For LCM: Code:
/* a & b are the numbers whose LCM is to be found */ Code:
/* a & b are the numbers whose GCD is to be found. 
Re: Finding LCM & GCD in C
For LCM
Code:
if(n%a == 0 && n%b == 0) Code:
if(a%n == 0 && b%n == 0) Code:
for(n=1;;n++) Code:
for(n=1;a%n == 0 && b%n == 0;n++); Code:
int gcd(int a,int b) 
Re: Finding LCM & GCD in C
Shabbir,
Least Common Multiple(L.C.M.) of 'a' and 'b' is the smallest number 'n' which is both perfectly divisible by 'a' as well as 'b'; i.e. n%a == 0 && n%b == 0. 
Re: Finding LCM & GCD in C
Right said Satyan, even I was about to post the same.
Thanks Shabbir for the modified code for the loop. 
Re: Finding LCM & GCD in C
Quote:

Re: Finding LCM & GCD in C
Quote:
The correct code would be: Code:
for(n=1;n%a != 0 && n%b != 0;n++); 
Re: Finding LCM & GCD in C
thanks buddy.....this might short'n my calcutaion

Re: Finding LCM & GCD in C
BlasterBlang, this is the third time I have been telling you to confine links to signatures only.

Re: Finding LCM & GCD in C
ok...fine

Re: Finding LCM & GCD in C
but link are not enable in signatures

All times are GMT +5.5. The time now is 01:04. 