A SHELL SCRIPT PROGRAM TO FIND GCD OF 2 NUMBERS - Code

Latest

programs

Wednesday, October 12, 2011

A SHELL SCRIPT PROGRAM TO FIND GCD OF 2 NUMBERS


A SHELL SCRIPT PROGRAM TO FIND GCD OF 2 NUMBERS
echo "..A SHELL SCRIPT PROGRAM TO FIND GCD OF 2 NUMBERS.."
echo "     INPUT     "
echo "-------------------"
echo "Enter Two Numbers :"
read n1
read n2
if [ $n1 -gt $n2 ]
then
t1=$n1
     t2=$n2
else
t1=$n2
t2=$n1
fi
re=1
while [ $re -ne 0 ]
do
re='expr $t1 % $t2'
t1=$t2
t2=$re
if [ $t1 -le $t2 ]
     then
t1=$t2
t2=$re
elif [ $re -eq 0 ]
     then
echo "The GCD  is $t1"
     fi
done


[root@localhost root]# sh gcd.sh
..... A SHELL SCRIPT PROGRAM TO FIND GCD OF 2 NUMBERS.."
INPUT
-----------
Enter Two Numbers:
11
43
GCD is 1



[root@localhost root]# sh gcd.sh
..... A SHELL SCRIPT PROGRAM TO FIND GCD OF 2 NUMBERS.."
INPUT
-----------
Enter Two Numbers:
8
4
GCD is 4

[root@localhost root]# sh gcd.sh
..... A SHELL SCRIPT PROGRAM TO FIND GCD OF 2 NUMBERS.."
INPUT
-----------
Enter Two Numbers:
12
4
GCD is 4

No comments:

Post a Comment