+ 2

quadratic equation code

i wrote a code to solve for roots of a quadratic equation. When i gave it values for this equation -> 6x^2 + 5x + 1, it gives answers as -12 and -18 , whereas when i solved using pen and paper answer should be -0.5 and -0.333 i must be making some silly mistake in the code , please can anybody point it out.. import java.util.Scanner; public class quadratic { public static void main(String[] args) { Scanner sc= new Scanner(System.in); System.out.println("Enter the co-effiecient of X^2"); int a=sc.nextInt(); System.out.println("Enter the co-effiecient of X"); int b = sc.nextInt(); System.out.println("Enter the constant"); int c= sc.nextInt(); double D= (b*b)-(4*a*c); System.out.println(D); double root1= ((-b) + (Math.sqrt(D)))/2*a; double root2= ((-b) - (Math.sqrt(D)))/2*a; System.out.println(root1); System.out.println(root2); } }

16th Jan 2022, 8:36 AM
PRAKHER SAXENA
3 odpowiedzi
+ 5
Hi PRAKHER SAXENA You need to put the 2*a in brackets to correct a order of operations issue because right now everything is divided by 2 then multipled by a not divided by 2a
16th Jan 2022, 8:45 AM
Ollie Q
Ollie Q - avatar
- 1
x
18th Jan 2022, 7:18 PM
منال ابراهيم
منال ابراهيم - avatar
- 1
(k);
18th Jan 2022, 7:19 PM
منال ابراهيم
منال ابراهيم - avatar