Evaluating a math expression in a Bash script
echo开发者_开发知识库 “Enter the value of variable x”
read x
echo “Enter the value of variable y”
read y
answer="\( $x + $y \) \* \( $x + $y \) \* \( $x + $y \) = $(( $x + $y ) * ($x + $y) * ($x + $y))"
I want a program to find (x+y)^3 and please let me know what should be the actual code
There is no need to use bc
, you can use Bash builtin arithmetics instead:
echo $((($x+$y)**3))
It can be done simply using bc
as:
$(echo "$(($x+$y))^3" | bc)
or simply using bash (thanks to lecodesportif):
$((($x+$y)**3))
You're missing a few parens.
Fixed:
answer="\( $x + $y \) \* \( $x + $y \) \* \( $x + $y \) = $(( ($x + $y ) * ($x + $y) * ($x + $y) ))"
I hope this helps.
P.S. as you appear to be a new user, if you get an answer that helps you please remember to mark it as accepted, and/or give it a + (or -) as a useful answer.
You could also use:
let ANSWER=(x+y)**3
精彩评论