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

Thanks in advance.

man5237
0
Newbie Poster

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

Thanks in advance.

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

Jump to PostIsn'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
5,138
Posting Sage

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

man5237
0
Newbie Poster

Project work

man5237
0
Newbie Poster

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.

Salem
5,138
Posting Sage

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
0
Newbie Poster

I tried but I am unable to write the program.

So please write the whole program for me.

Please.

Salem
5,138
Posting Sage

Did you try a few examples to see how it might work for you?

kvmreddy
0
Newbie Poster

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.