OneCompiler

Quadratic equation

239

import java.util.*;

public class QuadraticEquation {
public static void main(String []args) {
int D;
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("enter constant of x^2");

	    int a = myObj.nextInt();  // Read user input
	    System.out.println("Constant of x^2 is: " +a);  // Output user input
	    int b = myObj.nextInt();
	    System.out.println("constant of x is: " +b);
	    int c = myObj.nextInt();
	    System.out.println("constant value: "+c);
	    D=(b*b)-(4*a*c);
	    System.out.println("D is : "+D);
	    double F = Math.sqrt(D);
	    System.out.println("Square root is:" +F);
	    
	    
	    if(D>= 0) {
	    	double r1 = ((-b) + F)/(2*a);
	    	double r2 = ((-b) - F)/(2*a);
	    	System.out.println("Roots are : "+r1+","+r2);
	    
}
	    else {
	    	double rp = (-b)/(2*a);
	    	double ip = (-F)/(2*a);
	    	System.out.println("Real part and Imaginary part :"+rp+"+j("+ip+")");
	    }
	myObj.close();    

}
}