Wednesday 15 July 2015

Bash script for finding the square root of a number(Babylonian Method) -



Bash script for finding the square root of a number(Babylonian Method) -

i wrote code find square root of given x number, lastly part gives me "integer look expected" , can do? (i'm shell/bash noob)

#bin/bash 2 clear 3 echo "hello, calculate square root of number x" 4 echo "we're going utilize babylonian method" 5 echo "give me valor x" 6 read x 7 if [ $x -lt 0 ] 8 9 clear 10 echo "the roots of number imaginary" 11 elif [ $x -eq 0 ] 12 13 clear 14 echo "the square root of 0 0" 15 else 16 echo "now give me base of operations b , vertical height h bh=x" 17 18 echo "give me b" 19 read b 20 echo "give me h" 21 read h 22 fi 23 24 if [ $b -eq $h ] 25 26 echo:"b or h square root of h" 27 else 28 until [ $b -eq $h ] 29 30 b=`echo "scale=3; ($b + $h)/2"|bc -l` 31 h=`echo "scale=3; $x/$b"|bc -l` 32 done 33 fi 34 echo "the square root of x $b or $h" 35

bash can handle integers. same applies [ ... ] (check man test):

integer1 -eq integer2 integer1 equal integer2

in order compare floats, utilize bc correctly counting.

bash

No comments:

Post a Comment