Open In App

A Shell program To Find The GCD | Linux

The given problem asked in fico placement interview round. Find the gcd of two given numbers using shell scripting 
We are given two numbers A and B and now task is to find the Greatest Common divisor (gcd) of two given number using shell scripting .
Asked in interview : FICO
Example: 
 

Input
25 15
Output
5

 







The shell script for the above code is :

echo Enter two numbers with space in between
read a b
//reads numbers
m=$a
if [ $b -lt $m ]
then
m=$b
fi
while [ $m -ne 0 ]
do
x=`expr $a % $m`
y=`expr $b % $m`
if [ $x -eq 0 -a $y -eq 0 ]
then
echo gcd of $a and $b is $m
break
fi
m=`expr $m - 1`
done



 



Article Tags :