java: quadratic equation coefficients
Can you guys explain why the code don't work? :( Help please ax2 + bx + c = 0 -> return one or two roots of the equation if there is any in the set of real numbers -> Input value via System.in -> Output value must be printed to System.out Output format is: -> "x1 x2" (two roots in any order separated by space) if there are two roots -> "x" (just the value of the root) if there is the only root -> "no roots" (just a string value "no roots") if there is no root -> The a parameter is guaranteed to be not zero import java.util.Scanner; //import static java.lang.Math.sqrt; public class QuadraticEquation { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); double a = scanner.nextDouble(); double b = scanner.nextDouble(); double c = scanner.nextDouble(); double discriminant = Math.pow(b, 2) - 4 * a * c; System.out.print("The equation has "); if (discriminant > 0) { double root1 = (-b + Math.pow(discriminant, 0.5)) / (2 * a); double root2 = (-b - Math.pow(discriminant, 0.5)) / (2 * a); System.out.println("two roots " + root1 + " and " + root2); } else if (discriminant == 0) { double root1 = (-b + Math.pow(discriminant, 0.5)) / (2 * a); System.out.println("one root " + root1); } else System.out.println("no real roots"); } } I need to receive (for example): Input: 2, 5, -3 Output: -3 0.5 Input: 2, 2, 2 Output: no roots