DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Shell Scripting (http://www.daniweb.com/forums/forum113.html)
-   -   Shell program to find GCD (http://www.daniweb.com/forums/thread150389.html)

man5237 Oct 10th, 2008 11:45 am
Shell program to find GCD
 
Please help me to write a shell script to find the GCD (Greatest common Divisor) of 3 numbers.


Thanks in advance.

Salem Oct 10th, 2008 5:10 pm
Re: Shell program to find GCD
 
Why are you trying to do maths in the shell (which it really isn't set up to do).

man5237 Oct 11th, 2008 10:47 am
Re: Shell program to find GCD
 
Project work

man5237 Oct 13th, 2008 11:44 am
Re: Shell program to find GCD
 
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


Quote:

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.

Salem Oct 13th, 2008 5:48 pm
Re: Shell program to find GCD
 
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.

man5237 Oct 13th, 2008 11:16 pm
Re: Shell program to find GCD
 
I tried but I am unable to write the program.


So please write the whole program for me.


Please.

Salem Oct 14th, 2008 2:08 am
Re: Shell program to find GCD
 
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?


All times are GMT -4. The time now is 2:51 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC