Print a pair of numbers with the given Sum and Product | CPP 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findRoots ( int b , int c ) { int a = 1 ; int d = b * b - 4 * a * c ; double sqrt_val = sqrt ( abs ( d ) ) ; if ( d > 0 ) { double x = - b + sqrt_val ; double y = - b - sqrt_val ; int root1 = ( x ) / ( 2 * a ) ; int root2 = ( y ) / ( 2 * a ) ; if ( root1 + root2 == -1 * b && root1 * root2 == c ) cout << root1 << " , ▁ " << root2 ; else cout << -1 ; } else if ( d == 0 ) { int root = - b / ( 2 * a ) ; if ( root + root == -1 * b && root * root == c ) cout << root << " , ▁ " << root ; else cout << -1 ; } else { cout << -1 ; } cout << endl ; } int main ( ) { int S = 5 , P = 6 ; findRoots ( - S , P ) ; S = 5 , P = 9 ; findRoots ( - S , P ) ; return 0 ; }