User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Shell Scripting section within the Software Development category of DaniWeb, a massive community of 457,640 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,779 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Shell Scripting advertiser: Programming Forums
Views: 1102 | Replies: 6
Reply
Join Date: Apr 2008
Posts: 7
Reputation: man5237 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
man5237 man5237 is offline Offline
Newbie Poster

Shell program to find GCD

  #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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2005
Posts: 3,834
Reputation: Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of 
Rep Power: 23
Solved Threads: 436
Colleague
Salem's Avatar
Salem Salem is offline Offline
banned

Re: Shell program to find GCD

  #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  
Join Date: Apr 2008
Posts: 7
Reputation: man5237 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
man5237 man5237 is offline Offline
Newbie Poster

Re: Shell program to find GCD

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

Re: Shell program to find GCD

  #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  
Join Date: Dec 2005
Posts: 3,834
Reputation: Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of 
Rep Power: 23
Solved Threads: 436
Colleague
Salem's Avatar
Salem Salem is offline Offline
banned

Re: Shell program to find GCD

  #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  
Join Date: Apr 2008
Posts: 7
Reputation: man5237 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
man5237 man5237 is offline Offline
Newbie Poster

Re: Shell program to find GCD

  #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  
Join Date: Dec 2005
Posts: 3,834
Reputation: Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of 
Rep Power: 23
Solved Threads: 436
Colleague
Salem's Avatar
Salem Salem is offline Offline
banned

Re: Shell program to find GCD

  #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  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Shell Scripting Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the Shell Scripting Forum

All times are GMT -4. The time now is 4:11 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC