-
Notifications
You must be signed in to change notification settings - Fork 0
/
Arithmetic_Operations.sh
39 lines (38 loc) · 999 Bytes
/
Arithmetic_Operations.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/bin/bash
echo -e "Enter first num: \c"
read num1
echo -e "Enter second num: \c"
read num2
#For integers
echo sum= $(( num1 + num2))
echo sub= $(( num1 - num2 ))
echo mul= $(( num1 * num2 ))
echo div= $(( num1 / num2 ))
echo mod= $(( num1 % num2 ))
#Similarly, we can use expr keyword
echo sum= $( expr $num1 + $num2)
echo sub= $( expr $num1 - $num2 )
echo mul= $( expr $num1 \* $num2 )
echo div= $( expr $num1 / $num2 )
echo mod= $( expr $num1 % $num2 )
#For fractional integers
echo -e "Enter a num: \c"
read num1
echo -e "Enter second num: \c"
read num2
echo -e "sum=\c"
echo "$num1 + $num2"|bc
echo -e "sub=\c"
echo "$num1 - $num2"|bc
echo -e "mul=\c"
echo "$num1 * $num2"|bc
echo -e "div=\c"
echo "scale=2;$num1 / $num2"|bc
echo -e "mod=\c"
echo "$num1 % $num2"|bc #not include scale at mod
echo -e "Enter another num: \c"
read num
echo -e "sqrt of $num= \c"
echo "scale=2;sqrt($num)"|bc -l #-l to define the standard math library
echo -e "square of $num= \c"
echo "scale=2;$num*$num"|bc -l