Shell program to find GCD

Reply

Join Date: Apr 2008
Posts: 7
Reputation: man5237 is an unknown quantity at this point 
Solved Threads: 0
man5237 man5237 is offline Offline
Newbie Poster

Shell program to find GCD

 
0
  #1
Oct 10th, 2008
Please help me to write a shell script to find the GCD (Greatest common Divisor) of 3 numbers.


Thanks in advance.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,851
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Shell program to find GCD

 
0
  #2
Oct 10th, 2008
Why are you trying to do maths in the shell (which it really isn't set up to do).
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 7
Reputation: man5237 is an unknown quantity at this point 
Solved Threads: 0
man5237 man5237 is offline Offline
Newbie Poster

Re: Shell program to find GCD

 
0
  #3
Oct 11th, 2008
Project work
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 7
Reputation: man5237 is an unknown quantity at this point 
Solved Threads: 0
man5237 man5237 is offline Offline
Newbie Poster

Re: Shell program to find GCD

 
0
  #4
Oct 13th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,851
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Shell program to find GCD

 
0
  #5
Oct 13th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 7
Reputation: man5237 is an unknown quantity at this point 
Solved Threads: 0
man5237 man5237 is offline Offline
Newbie Poster

Re: Shell program to find GCD

 
0
  #6
Oct 13th, 2008
I tried but I am unable to write the program.


So please write the whole program for me.


Please.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,851
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Shell program to find GCD

 
0
  #7
Oct 14th, 2008
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?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC