LCM of two numbers

You are given an inbuilt function GCD(int ,int) to generate GCD of two numbers.

public int GCD(int a, int b)
{
   if (b==0) return a;
   return GCD(b,a%b);
}
  • Generate a function LCM(int , int) using above function.
  • How will you use the same function to find LCM of
more than 2 numbers.

Comments

  1. long LCM(int a, int b){
    return b*(a/GCD(a,b));
    }


    //let a[0..n-1] be an array
    int r=a[0];
    for(i=1;i<n;i++){
    r=LCM(r,a[i]);
    }
    cout<<r;

    ReplyDelete

Post a Comment

Popular posts from this blog