Program to find the Roots of Quadratic equation | Java program to find roots of a quadratic equation ; Prints roots of quadratic equation ax * 2 + bx + x ; If a is 0 , then equation is not quadratic , but linear ; else d < 0 ; Driver code ; Function call
import java . io . * ; import static java . lang . Math . * ; class Quadratic { static void findRoots ( int a , int b , int c ) { if ( a == 0 ) { System . out . println ( " Invalid " ) ; return ; } int d = b * b - 4 * a * c ; double sqrt_val = sqrt ( abs ( d ) ) ; if ( d > 0 ) { System . out . println ( "Roots are real and different NEW_LINE"); System . out . println ( ( double ) ( - b + sqrt_val ) / ( 2 * a ) + "NEW_LINE" + ( double ) ( - b - sqrt_val ) / ( 2 * a ) ) ; } else if ( d == 0 ) { System . out . println ( "Roots are real and same NEW_LINE"); System . out . println ( - ( double ) b / ( 2 * a ) + "NEW_LINE" + - ( double ) b / ( 2 * a ) ) ; } { System . out . println ( "Roots are complex NEW_LINE"); System . out . println ( - ( double ) b / ( 2 * a ) + " ▁ + ▁ i " + sqrt_val + "NEW_LINE" + - ( double ) b / ( 2 * a ) + " ▁ - ▁ i " + sqrt_val ) ; } } public static void main ( String args [ ] ) { int a = 1 , b = - 7 , c = 12 ; findRoots ( a , b , c ) ; } }