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.01493

Input:N = 1

Output:NO

**Approach:** If observed carefully, we are given with sum and product of roots of a quadratic equation.

If *N ^{2} – 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(

N) ) / 2^{2}– 4*N

b = ( N – sqrt(N) ) / 2^{2}– 4*N

Below is the implementation of the above approach:

## C++

`// 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 string ` `void` `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 Code ` `int` `main() ` `{ ` ` ` `double` `N = 69.0; ` ` ` `findAandB(N); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find a and b ` `// such that a*b=N and a+b=N ` ` ` `class` `GFG{ ` `// Function to return the smallest string ` `static` `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 Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `double` `N = ` `69.0` `; ` ` ` `findAandB(N); ` `} ` `} ` `// This Code is contributed by mits ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 program to find a and b ` `# such that a*b=N and a+b=N ` `from` `math ` `import` `sqrt ` ` ` `# Function to return the ` `# smallest string ` `def` `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 Code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `N ` `=` `69.0` ` ` `findAandB(N) ` ` ` `# This code is contributed ` `# by SURENDRA_GANGWAR ` |

*chevron_right*

*filter_none*

## C#

`// 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 string ` `static` `void` `findAandB(` `double` `N) ` `{ ` `double` `val = N * N - 4.0 * N; ` ` ` `// Not possible ` `if` `(val < 0) { ` `Console.WriteLine(` `"NO"` `); ` `return` `; ` `} ` ` ` `// find a and b ` `double` `a = (N + Math.Sqrt(val)) / 2.0; ` `double` `b = (N - Math.Sqrt(val)) / 2.0; ` ` ` `Console.WriteLine(` `"a = "` `+a); ` `Console.WriteLine(` `"b = "` `+b); ` `} ` ` ` `// Driver Code ` `static` `void` `Main() ` `{ ` `double` `N = 69.0; ` `findAandB(N); ` `} ` `// This code is contributed by ANKITRAI1 ` `} ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find a and b ` `// such that a*b=N and a+b=N ` ` ` `// Function to return the ` `// smallest string ` `function` `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 ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

a = 67.9851 b = 1.01493

## Recommended Posts:

- Find two numbers with difference and division both same as N
- Divide array in two Subsets such that sum of square of sum of both subsets is maximum
- Maximize product of same-indexed elements of same size subsequences
- Find all numbers up to N which are both Pentagonal and Hexagonal
- Maximum length of subarray consisting of same type of element on both halves of sub-array
- Loss when two items are sold at same price and same percentage profit/loss
- Program for dot product and cross product of two vectors
- Check whether two strings contain same characters in same order
- Numbers in range [L, R] such that the count of their divisors is both even and prime
- Count numbers upto N which are both perfect square and perfect cube
- Numbers less than N which are product of exactly two distinct prime numbers
- Sum of the products of same placed digits of two numbers
- Check if sum of divisors of two numbers are same
- Sum of series formed by difference between product and sum of N natural numbers
- Find ΔX which is added to numerator and denominator both of fraction (a/b) to convert it to another fraction (c/d)
- Choose points from two ranges such that no point lies in both the ranges
- Partition an array of non-negative integers into two subsets such that average of both the subsets is equal
- Check whether two strings can be made equal by reversing substring of equal length from both strings
- Find two distinct prime numbers with given product
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.