In this **HackerRank Arithmetic Operations problem solution** A mathematical expression containing +,-,*,^, / and parenthesis will be provided. Read in the expression, then evaluate it. Display the result rounded to 3 decimal places.

**Constraints**

All numeric values are <= 999.

## Problem solution.

read line ans1=`(echo "scale = 4; ($line)" | bc)` IFS='.' read -a fnum <<< "$ans1" dig="${fnum[1]:3}" mantissa=`echo "${fnum[1]}" |cut -c1-3` if [ "$dig" -ge "5" ] then mantissa=$((mantissa+1)) fi ans=${fnum[0]}$"."$mantissa echo $ans

## Second solution.

read line val=`echo "$line" | bc -l` if [ `echo "$val < 0.0" | bc` -eq 1 ]; then echo "scale = 3; ($val - 0.0005) / 1.0" | bc -l else echo "scale = 3; ($val + 0.0005) / 1.0" | bc -l fi

## Third solution.

#!/bin/bash # your code goes here read n printf "%.3f\n" $(bc -l <<< $n)

## Fourth solution.

read expr value=$(echo "$expr" | bc -lq) printf '%.3f' $value

## 0 Comments