Friday, 3 December 2010

find gcd and lcm of 2 numbers

find gcd and lcm of 2 numbers

int gcd(int m,int n)
{
 while(m!=n)
 {
/*large-smallest, store the result in largest variable i.e: if m is greater than n, then store result in m  else otetherwise store result in n
*/
   if(m>n)
    //if m is greater than n, largest values-smallest, result is in largest (m)
  else
  //if m<n, smallest value (n)-large value, result in smallest(n)

   n=n-m;
 }
 return(m);//m,n is gcd here, so we can write return(n) also
}

No comments:

Post a Comment