| | |
Shell program to find GCD
![]() |
•
•
Join Date: Apr 2008
Posts: 7
Reputation:
Solved Threads: 0
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
So please write a program to find the GCD of 3 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.
![]() |
Other Threads in the Shell Scripting Forum
- Previous Thread: Shell script accepting input from a pipe AND the keyboard
- Next Thread: How to bulk resize JPG files in folder and subfolder
| Thread Tools | Search this Thread |






