Write expressions to compute both solutions for the quadratic formula. The formula is
-b+-square root b^2-4ac/2aThe +- symbol means "plus or minus" and indicates that the equation has two solutions:one in which the result of the square root is added to 2a and one in which the result is subtracted from 2a. Assume all variables are float variables.
Code
int a = 1;
int b = 3;
int c = 2;
float x1 = (((-1)*b)-(sqrt(sq(b)-(4*a*c))))/(2*a);
float x2 = (((-1)*b)+(sqrt(sq(b)-(4*a*c))))/(2*a);
println (x1);
println (x2);
Output
-2.0
-1.0
Text book name : Programming and Problem solving with Java second edition
Author : Nell Dale and Chip Weems
ISBN : 978-0-7637-3402-2
ไม่มีความคิดเห็น:
แสดงความคิดเห็น