Answer to Question #239001 in Java | JSP | JSF for Woyengimiesindo Ma

Question #239001
The roots of the quadratic equation ax2 + bx + c = 0, a
1
Expert's answer
2021-09-22T00:05:26-0400


package quadraticequation;
import java.util.Scanner;
public class QuadraticEquation {
  
    public static void main(String[] args)
    {
  
      
        
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter the value of a\n");
        double a_coff = scan.nextDouble();
        System.out.println("Enter the value of b\n");
        double b_coff = scan.nextDouble();
        
        System.out.println("Enter the value of c\n");
        double c_coff = scan.nextDouble();
        
       
        double root1, root2;
  
        
        double determinant = b_coff * b_coff - 4 * a_coff * c_coff;
  
       
        if (determinant > 0) {
  
            
            root1 = (-b_coff + Math.sqrt(determinant)) / (2 * a_coff);
            root2= (-b_coff - Math.sqrt(determinant)) / (2 * a_coff);
  
        }
  
       
        else if (determinant == 0) {
  
            
            root1 = root2 = -b_coff / (2 * a_coff);
  
          
        }
  
       
        else {
  
            
            root1 = -b_coff / (2 * a_coff);
  
            root2 = Math.sqrt(-determinant) / (2 * a_coff);
  
           
        }
        
        System.out.println("Root 1: "+root1+"\nRoot 2:  "+root2);
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS