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


Thanks in advance.

Recommended Answers

All 7 Replies

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"
read n1
echo "Enter the second number"
read n2
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.


Please.

So did you do ANY research on how to write a function in shell?
Did you try a few examples to see how it might work for you?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.