Please help me to write a shell script to find the GCD (Greatest common Divisor) of 3 numbers.

## All 7 Replies

Why are you trying to do maths in the shell (which it really isn't set up to do).

Project work

I got a program to find the GCD of 2 numbers. But i need the program to find the GCD of three numbers.

Program to find the GCD of 2 numbers is as follows

echo "Enter first number"
echo "Enter the second number"
gcd=0
if test \$n1 -gt \$n2
then
i=1
while test \$i -le \$n1
do
a=`expr \$n1 % \$i`
b=`expr \$n2 % \$i`
if test \$a -eq 0 -a \$b -eq 0
then
if test \$gcd -lt \$i
then
gcd=\$i
fi
fi
i=`expr \$i + 1`
done
fi
if test \$n2 -gt \$n1
then
i=1
while test \$i -le \$n2
do
a=`expr \$n1 % \$i`
b=`expr \$n2 % \$i`
if test \$a -eq 0 -a \$b -eq 0
then
if test \$gcd -lt \$i
then
gcd=\$i
fi
fi
i=`expr \$i + 1`
done
fi
echo GCD of \$n1 and \$n2 = \$gcd

So please write a program to find the GCD of 3 numbers.

Isn't that just gcd(a,gcd(b,c))

> So please write a program to find the GCD of 3 numbers.
No, we suggest, you do it.

My suggestion is you look to see if your shell supports functions, which you can call in the manner I've described.

I tried but I am unable to write the program.

So please write the whole program for me.