Find two numbers with sum and product both same as N - GeeksforGeeks

10 May, 2021
Given an integer N, the task is to find two numbers a and b such that a * b = N and a + b = N. Print “NO” if no such numbers are possible. Examples:  
Input: N = 69 Output: a = 67.9851 b = 1.01493Input: N = 1 Output: NO  
 
Approach: If observed carefully, we are given with sum and product of roots of a quadratic equation. If N2 – 4*N < 0 then only imaginary roots are possible for the equation, hence “NO” will be the answer. Else a and b will be:  
a = ( N + sqrt( N2 – 4*N ) ) / 2 b = ( N – sqrt( N2 – 4*N ) ) / 2  
Below is the implementation of the above approach:  
C++
Java
Python3
C#
PHP
Javascript
// C++ program to find a and b// such that a*b=N and a+b=N#include <bits/stdc++.h>using namespace std; // Function to return the smallest stringvoid findAandB(double N){    double val = N * N - 4.0 * N;     // Not possible    if (val < 0) {        cout << "NO";        return;    }     // find a and b    double a = (N + sqrt(val)) / 2.0;    double b = (N - sqrt(val)) / 2.0;     cout << "a = " << a << endl;    cout << "b = " << b << endl;} // Driver Codeint main(){    double N = 69.0;    findAandB(N);    return 0;}
// Java program to find a and b// such that a*b=N and a+b=N class GFG{// Function to return the smallest stringstatic void findAandB(double N){    double val = N * N - 4.0 * N;     // Not possible    if (val < 0) {        System.out.println("NO");        return;    }     // find a and b    double a = (N + Math.sqrt(val)) / 2.0;    double b = (N - Math.sqrt(val)) / 2.0;     System.out.println("a = "+a);    System.out.println("b = "+b);} // Driver Codepublic static void main(String[] args){    double N = 69.0;    findAandB(N);}}// This Code is contributed by mits
# Python 3 program to find a and b# such that a*b=N and a+b=Nfrom math import sqrt # Function to return the# smallest stringdef findAandB(N):    val = N * N - 4.0 * N     # Not possible    if (val < 0):        print("NO")        return         # find a and b    a = (N + sqrt(val)) / 2.0    b = (N - sqrt(val)) / 2.0     print("a =", '{0:.6}' . format(a))    print("b =", '{0:.6}' . format(b)) # Driver Codeif __name__ == '__main__':    N = 69.0    findAandB(N)     # This code is contributed# by SURENDRA_GANGWAR   
// C# program to find a and b// such that a*b=N and a+b=N using System;class GFG{// Function to return the smallest stringstatic void findAandB(double N){double val = N * N - 4.0 * N; // Not possibleif (val < 0) {Console.WriteLine("NO");return;} // find a and bdouble a = (N + Math.Sqrt(val)) / 2.0;double b = (N - Math.Sqrt(val)) / 2.0; Console.WriteLine("a = "+a);Console.WriteLine("b = "+b);} // Driver Codestatic void Main(){double N = 69.0;findAandB(N);}// This code is contributed by ANKITRAI1}
<?php// PHP program to find a and b// such that a*b=N and a+b=N // Function to return the// smallest stringfunction findAandB($N){    $val = $N * $N - 4.0 * $N;     // Not possible    if ($val < 0)    {        echo "NO";        return;    }     // find a and b    $a = ($N + sqrt($val)) / 2.0;    $b = ($N - sqrt($val)) / 2.0;     echo "a = " , $a, "\n";    echo "b = " , $b, "\n";} // Driver Code$N = 69.0;findAandB($N); // This code is contributed by ajit?>
<script>    // Javascript program to find a and b    // such that a*b=N and a+b=N         // Function to return the smallest string    function findAandB(N)    {      let val = N * N - 4.0 * N;       // Not possible      if (val < 0) {        document.write("NO");        return;      }       // find a and b      let a = (N + Math.sqrt(val)) / 2.0;      let b = (N - Math.sqrt(val)) / 2.0;       document.write("a = "+a.toFixed(4) + "</br>");      document.write("b = "+b.toFixed(5));    }         let N = 69.0;    findAandB(N);         </script>
a = 67.9851
b = 1.01493
 
Mithun Kumar
ankthon
jit_t
SURENDRA_GANGWAR
divyesh072019
Algebra
Competitive Programming
Mathematical
School Programming
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Pairs with same Manhattan and Euclidean distance
Remove all occurrences of a character in a string | Recursive approach
Breadth First Traversal ( BFS ) on a 2D array
Most important type of Algorithms
Sequence Alignment problem
Program for Fibonacci numbers
Write a program to print all permutations of a given string
C++ Data Types
Set in C++ Standard Template Library (STL)
Program to find sum of elements in a given array