Tuesday 28 July 2020

Shell script to find the LCM and GCD of two numbers


PROGRAM
echo enter two numbers
read m n
echo "The given numbers are \c"
echo $m and $n
temp=`expr $m \* $n`
while [ $m -ne $n ]
do
    if [ $m -gt $n ]
    then
        m=`expr $m - $n`
    else
        n=`expr $n - $m`
    fi
done
echo GCD = $n
lcm=`expr $temp / $n`
echo LCM = $lcm



OUTPUT
enter two numbers
24 60
The given numbers are 24 and 60
GCD = 12
LCM = 120

No comments:

Post a Comment