How to solve a Quadratic Equation in Java
package quardatic_equcation;
/**
*
* @author IPHONE14
*/
public class Quardatic_equcation {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
//progtram to calculate quardatic equcation in java programming language
double a = 2.3, b = 4, c = 5.6;
double root_1, root_2;
double determinant = b *b -4*a*c;
if(determinant>0){
root_1 = (-b+Math.sqrt(determinant))/(2*a);
root_2 = (-b+Math.sqrt(determinant))/(2*a);
System.out.format("root_1 = %.2f and root_2 = %.2f", root_1,root_2);
}else if(determinant == 0){
root_1 = root_2 = -b/(2*a);
System.out.format("root_1 = root_2 = %.2f;",root_1);
}else{
double real = -b/(2*a);
double imaginary = Math.sqrt(-determinant)/(2*a);
System.out.format("root_1 = %.2f +%.2fi", real, imaginary);
System.out.format("\nroot_2 = %.2f +%.2fi", real, imaginary);
}
}
}
Comments
Post a Comment