943,682 Members | Top Members by Rank

Ad:
Oct 10th, 2008
0

Shell program to find GCD

Expand Post »
Please help me to write a shell script to find the GCD (Greatest common Divisor) of 3 numbers.


Thanks in advance.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
man5237 is offline Offline
7 posts
since Apr 2008
Oct 10th, 2008
0

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).
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Oct 11th, 2008
0

Re: Shell program to find GCD

Project work
Reputation Points: 10
Solved Threads: 0
Newbie Poster
man5237 is offline Offline
7 posts
since Apr 2008
Oct 13th, 2008
0

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
man5237 is offline Offline
7 posts
since Apr 2008
Oct 13th, 2008
0

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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Oct 13th, 2008
0

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
man5237 is offline Offline
7 posts
since Apr 2008
Oct 14th, 2008
0

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?
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
May 9th, 2010
-1
Re: Shell program to find GCD
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kvmreddy is offline Offline
1 posts
since May 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Shell Scripting Forum Timeline: Dynamically searching a directory tree?
Next Thread in Shell Scripting Forum Timeline: zsh string compare help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC