Print a pair of numbers with the given Sum and Product | Prints roots of quadratic equation ax * 2 + bx + c = 0 ; calculating the sq root value for b * b - 4 * a * c ; Finding the roots ; Check if the roots are valid or not ; Finding the roots ; Check if the roots are valid or not ; when d < 0 ; No such pair exists in this case ; Driver code
function findRoots ( b , c ) { var a = 1 ; var d = b * b - 4 * a * c ; var sqrt_val = Math . sqrt ( Math . abs ( d ) ) ; if ( d > 0 ) { var x = - b + sqrt_val ; var y = - b - sqrt_val ; var root1 = ( x ) / ( 2 * a ) ; var root2 = ( y ) / ( 2 * a ) ; if ( root1 + root2 == - 1 * b && root1 * root2 == c ) document . write ( root1 + " " + root2 ) ; else document . write ( - 1 ) ; } else if ( d == 0 ) { var root = - b / ( 2 * a ) ; if ( root + root == - 1 * b && root * root == c ) document . write ( root + " " + root ) ; else document . write ( - 1 ) ; } else { document . write ( - 1 ) ; } document . write ( " " ) ; } var S = 5 , P = 6 ; findRoots ( - S , P ) ; S = 5 , P = 9 ; findRoots ( - S , P ) ;