Impact-Site-Verification: dbe48ff9-4514-40fe-8cc0-70131430799e

Search This Blog

#Day88#100DaysChallenge- Matlab Loops | LCM of Two Numbers

#Day88-LCM of Two Number

Task:

Write a code to Find LCM of two Number
 LCM(12,30)=60


Note: This code can be done using the in-built command. But for the challenge, I am trying to avoid those.


Matlab code

function lcmvalue=lcmoftwonumbers(a,b)

gcd=gcfoftwonumbers(a,b);

lcmvalue=(a*b)/gcd;

end

 

function num_gcf=gcfoftwonumbers(a,b)

 

if a>b

    max=a;

    min=b;

else

    max=b;

    min=a;

end

rem=mod(max,min);

if rem==0

    num_gcf=min;

else

    while rem>=1

        min_copy=rem;

        rem=mod(min,rem);

        min=min_copy;

    end

    num_gcf=min_copy;

       

   

end

Sample Input and output

> lcmoftwonumbers(12,30)


ans =


    60


>> lcmoftwonumbers(15,10)


ans =


    30


>> lcmoftwonumbers(71,63)


ans =


        4473


>> lcmoftwonumbers(71,23)


ans =


        1633

Click here for Video description





Live Support: t.me/matlabirawen



 

 


No comments

Popular Posts

Followers