954,546 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

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.

man5237
Newbie Poster
7 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

Why are you trying to do maths in the shell (which it really isn't set up to do).

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

Project work

man5237
Newbie Poster
7 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

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.

man5237
Newbie Poster
7 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

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.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

I tried but I am unable to write the program.


So please write the whole program for me.


Please.

man5237
Newbie Poster
7 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

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?

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 
kvmreddy
Newbie Poster
1 post since May 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You