how to do quadratic formula in java

Solutions on MaxInterview for how to do quadratic formula in java by the best coders in the world

showing results for - "how to do quadratic formula in java"
Paolo
25 Nov 2020
1public class Main {
2
3  public static void main(String[] args) {
4
5    // value a, b, and c
6    double a = 2.3, b = 4, c = 5.6;
7    double root1, root2;
8
9    // calculate the determinant (b2 - 4ac)
10    double determinant = b * b - 4 * a * c;
11
12    // check if determinant is greater than 0
13    if (determinant > 0) {
14
15      // two real and distinct roots
16      root1 = (-b + Math.sqrt(determinant)) / (2 * a);
17      root2 = (-b - Math.sqrt(determinant)) / (2 * a);
18
19      System.out.format("root1 = %.2f and root2 = %.2f", root1, root2);
20    }
21
22    // check if determinant is equal to 0
23    else if (determinant == 0) {
24
25      // two real and equal roots
26      // determinant is equal to 0
27      // so -b + 0 == -b
28      root1 = root2 = -b / (2 * a);
29      System.out.format("root1 = root2 = %.2f;", root1);
30    }
31
32    // if determinant is less than zero
33    else {
34
35      // roots are complex number and distinct
36      double real = -b / (2 * a);
37      double imaginary = Math.sqrt(-determinant) / (2 * a);
38      System.out.format("root1 = %.2f+%.2fi", real, imaginary);
39      System.out.format("\nroot2 = %.2f-%.2fi", real, imaginary);
40    }
41  }
42}
queries leading to this page
quadratic equation code in javawrite a java program to solve quadratic equationsjava quadratic equationimplement quadratic function in javahow to plug in a quadratic equation in javahow to do quadratic formula in javaquadratic equation javajava quadratic formulahow to do quadratic equations in javahow to calculate quadratic equation using javafinding roots of quadartic equation in javahow to solve equation on javacalculating quadratic equation javaquadratic formula java codequadratic formula function javacomplex roots in javahow to use quadratic formula in java write a java program to solve quadratic equations 28use if 2c else if and else 29 test data input a 3a 1 input b 3a 5 input c 3a 1 expected output 3a the roots are 0 20871215252208009 and 4 7912878474779195 java quadraticquadratic equation code javaquadratic formula impllemented in javaquadratic formula java with individual functionsjava program to solve quadratic equationhow to write quadratic equation in javaquadratic formula javaquadratic javajava solutions of a quadratichow to write a java program to solve quadratic equationwrite quadratic equation in javaprogram to find the quadratic equation in javaquadratic equation java codequadratic formula java with functionsquadratic formula method in javaquadratic equation java programjava program to find roots of a quadratic equationquadratic formula in javaroots of quadratic equation in javajava code for quadratic equation with degree 2quadratic equation in javahow to find quadratic equation in javafind solution for quadratic equation javaprogram to solve quadratic equation in javahow to do quadratic formula in java