3 October 2014

Quiz 34: Find GCD of 2 numbers

Problem:
Take 2 numbers as input and print their GCD

Input Format: 
number 1
number 2

Output Format: 
GCD

Constraints: 
none

Sample Input
6
9

Sample Output:
GCD is 3

Explanations:
3 is GCD of 6 and 9


Solution:

chomp($a=<STDIN>);
chomp($b=<STDIN>);
while($b)
{
($a, $b) = ($b, $a % $b)
}
print "GCD is $a";


Tips:
Learn the logic of GCD

No comments:

Post a Comment