Branch-18. Finding the root of the quadratic equation

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.