This topic requires the root of the quadratic equation A * X2 + B * x + c = 0, and the result is retained with two decimal places.
Input Format:
Input three floating point coefficients A, B, and C in one row, separated by spaces.
Output Format:
Different results are output based on the coefficients:
1) if the equation has two unequal real number roots, one root is output for each row, which is first large, then small. 2) if the equation has two unequal complex root numbers, then, each line outputs a root according to the format "Real + virtual I". First, the imaginary part is positive, and then the imaginary part is negative. 3) if the equation has only one root, 4) if the coefficient is 0, the output is "Zero Equation"; 5) If A and B are 0, C is not 0, the output is "not an equation ".
import java.text.DecimalFormat;import java.util.Scanner;public class Main{ public static void main(String[] args) { DecimalFormat df = new DecimalFormat("0.00"); Scanner input = new Scanner(System.in); String str = input.nextLine(); String[] strs = str.split(" "); double a,b,c; a = Double.parseDouble(strs[0]); b = Double.parseDouble(strs[1]); c = Double.parseDouble(strs[2]); if(a == 0 && b == 0 && c == 0) System.out.printf("Zero Equation"); else if(a == 0 && b == 0 && c != 0) System.out.printf("Not An Equation"); else if(a == 0 && b != 0 && c != 0) System.out.printf(df.format(-c/b)); else if(b*b - 4*a*c > 0) System.out.printf(df.format((-b+Math.sqrt(b*b - 4*a*c))/(2*a))+"\n"+df.format((-b-Math.sqrt(b*b - 4*a*c))/(2*a))); else if(b*b - 4*a*c < 0) System.out.printf(df.format(-b/(2*a))+"+"+df.format(Math.sqrt(-b*b + 4*a*c)/(2*a))+"i"+"\n" +df.format(-b/(2*a))+"-"+df.format(Math.sqrt(-b*b + 4*a*c)/(2*a))+"i"); else System.out.printf(df.format(-b/(2*a))); }}
Branch-18. Finding the root of the quadratic equation