Interviews Questions, Algorithms, Aptitude, C Interview Program, C Theory Question, Aptitude Tricks, Test Series,

Friday, 3 May 2019

IOPP Program 2 B

Question:
Write a shell script to find the GCD and LCM of two numbers. 

Solution:
echo "Enter the first number:"
read a
echo "Enter the second number : "
read b

if [ $a -gt $b ]
then
num=$a
den=$b
else
num=$b
den=$a
fi
r=`expr $num % $den`

while [ $r -ne 0 ]
do
num=$den
den=$r
r=`expr $num % $den`
done

gcd=$den
lcm=`expr $a \* $b / $gcd`

echo " The LCM of $a and $b is : $lcm"
echo " The GCD of $a and $b is : $gcd"

0 comments:

Post a Comment