Computer Science, asked by ldmerani, 6 months ago

Write a Java program for the following class design
class
Quadratic
void roots_1(int aint b,intc)
Print the values of a, b and c.
Also the roots of the quadratic
equation given the coefficient of
x²x
and constant as a,b and c
respectively
using the formula
T
-b+86² – 4ac
2a
void roots_2(int aint b,intc)
Print the values of a, b and c.
Also the roots of the quadratic
equation given the coefficient of
x² x
and constant as a,b and c
respectively
using the formula
--- Vb2 - 4ac
2a​

Answers

Answered by mishraaditya29024
1

public class Main {

public static void main(String[] args) {

// value a, b, and c

double a = 2.3, b = 4, c = 5.6;

double root1, root2;

// calculate the determinant (b2 - 4ac)

double determinant = b * b - 4 * a * c;

// check if determinant is greater than 0

if (determinant > 0) {

// two real and distinct roots

root1 = (-b + Math.sqrt(determinant)) / (2 * a);

root2 = (-b - Math.sqrt(determinant)) / (2 * a);

System.out.format("root1 = %.2f and root2 = %.2f", root1, root2);

}

// check if determinant is equal to 0

else if (determinant == 0) {

// two real and equal roots

// determinant is equal to 0

// so -b + 0 == -b

root1 = root2 = -b / (2 * a);

System.out.format("root1 = root2 = %.2f;", root1);

}

// if determinant is less than zero

else {

// roots are complex number and distinct

double real = -b / (2 * a);

double imaginary = Math.sqrt(-determinant) / (2 * a);

System.out.format("root1 = %.2f+%.2fi", real, imaginary);

System.out.format("\nroot2 = %.2f-%.2fi", real, imaginary);

}

}

}

Output

root1 = -0.87+1.30i and root2 = -0.87-1.30i

Similar questions