3 October 2014

Quiz 35: Find LCM of 2 numbers

Problem:
Take 2 numbers as input and print their LCM

Input Format: 
number 1
number 2

Output Format: 
LCM

Constraints: 
none

Sample Input
15
20

Sample Output:
LCM is 60

Explanations:
60 is LCM of 15 and 20


Solution:

chomp($x=<STDIN>);
chomp($y=<STDIN>);
($a,$b) =($x,$y);
while($b)
{
($a, $b) = ($b, $a % $b)
}
$gcd=$a;
$lcm=($x*$y)/$gcd;
print "LCM is $lcm";


Tips:
LCM= product of numbers/GCD

No comments:

Post a Comment