Print a pair of numbers with the given Sum and Product | Java program to find any pair which has sum S and product P . ; 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
import java . util . * ; class GFG { static void findRoots ( int b , int c ) { int a = 1 ; int d = b * b - 4 * a * c ; double sqrt_val = Math . sqrt ( Math . abs ( d ) ) ; if ( d > 0 ) { double x = - b + sqrt_val ; double y = - b - sqrt_val ; int root1 = ( int ) ( x ) / ( 2 * a ) ; int root2 = ( int ) ( y ) / ( 2 * a ) ; if ( root1 + root2 == - 1 * b && root1 * root2 == c ) System . out . print ( root1 + " , ▁ " + root2 ) ; else System . out . print ( - 1 ) ; } else if ( d == 0 ) { int root = - b / ( 2 * a ) ; if ( root + root == - 1 * b && root * root == c ) System . out . print ( root + " , ▁ " + root ) ; else System . out . print ( - 1 ) ; } else { System . out . print ( - 1 ) ; } System . out . println ( ) ; } public static void main ( String [ ] args ) { int S = 5 , P = 6 ; findRoots ( - S , P ) ; S = 5 ; P = 9 ; findRoots ( - S , P ) ; } }