x=$1
y=$2
m=$x
if [ $y -lt $m ]
then
m=$y
fi
while [ $m -ne 0 ]
do
a=`expr $x % $m`
b=`expr $y % $m`
if [ $a -eq 0 -a $b -eq 0 ]
then
echo $m
break
fi
m=`expr $m - 1`
done
z=`expr $x / $m`
w=`expr $y / $m`
echo "$z / $w"
I have done for two number taking as taking numerator and denominator for a fraction from user.
But how to reuse this to get Again take numerator and denominator from user. In this case if numerator is less than denominator then print smallest possible integer numerator and denominator for same fraction. However if numerator is bigger than denominator then print mixed fractions where fraction part again should have smallest possible integer numerator and denominator.
For example if user enters 44, 10 then output should be 4, 2, 5 as 44/10=4 2/5
((…))
or$((…))
, the latter being POSIX-compliant and the former not, but they're remarkably similar in many ways. Usingexpr
is almost always a mistake — especially if you're using Bash — unless there's a compelling portability issue to deal with. – Jonathan Leffler Jul 30 '15 at 4:17