#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
Free Codes: youtube.com/castorclasses
No comments