Posted by: yekloyatri July 18, 2007
Login in to Rate this Post:
0
?
import java.util.*;
import java.io.*;
public class quadraticEquation{
public static void main(String[] args){
Scanner Keyboard = new Scanner(System.in);
double a, b, c, discriminant, rootA, rootB;
System.out.println("Please enter a positive value for a. ");
a = Keyboard.nextDouble();
System.out.println("Please enter a positive value for b .");
b = Keyboard.nextDouble();
System.out.println("Please enter a positive value for c .");
c = Keyboard.nextDouble();
discriminant = (Math.pow(b, 2)) - (4*a*c);
if(discriminant == 0.0) {
rootA = (-b + Math.sqrt(discriminant)) / (2 *a);
System.out.println("There is one root and the value of the root is: " + rootA);
}
else {
if(discriminant > 0.0) {
rootA = (-b + Math.sqrt(discriminant)) / (2 * a);
rootB = (-b - Math.sqrt(discriminant)) / (2 * a);
System.out.println("There are two real roots and the value of the roots are:"+ rootA + rootB);
}
else {
if(discriminant < 0.0) {
rootA = (-b + Math.sqrt(-discriminant)) / (2 * a);
rootB = (-b + Math.sqrt(-discriminant)) / (2 * a);
System.out.println("There are two imaginary roots and the value of the roots are: " + rootA + rootB);
}
}
}
System.exit(0);
}
}