Search Here

Shell Script to calculate GCD of two positive numbers a and b. Numbers are supplied as command line arguments


if [ $# -ne 2 ]
then
    echo -e "\nInvalid numbers of arguments"
else
    a=$1
    b=$2      
    if [ $a -le 0 -o $b -le 0  ]
    then
        echo -e "\nFirst Argument or Second Argument is Zero or Negative"
    else
        c=`expr $a \* $b`          
        while [ $a -ne $b ]
        do
            if [ $a -gt $b ]
            then
                a=`expr $a - $b`
            else
                b=`expr $b - $a`
            fi
        done
        echo -e "\nGCD is $a\n"
    fi
fi      

No comments:

Post a Comment