- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures

- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces

# Find two numbers with sum and product both same as N in C++ Program

In this tutorial, we are going to write a program to find two numbers where x + y = n and x * y = n. Sometimes it's not possible to find those types of numbers. We'll print **None **if there are no such numbers. Let's get started.

The given numbers are the sum and products of a quadratic equation. So the number doesn't exist if n^{2} - 4*n<0.Else the numbers will be $$\lgroup n + \sqrt n^{2} - 4*n\rgroup/2$$ and $$\lgroup n - \sqrt n^{2} - 4*n\rgroup/2$$.

## Example

Let's see the code.

#include <bits/stdc++.h> using namespace std; void findTwoNumbersWithSameSumAndProduc(double n) { double imaginaryValue = n * n - 4.0 * n; // checking for imaginary roots if (imaginaryValue < 0) { cout << "None"; return; } // printing the x and y cout << (n + sqrt(imaginaryValue)) / 2.0 << endl; cout << (n - sqrt(imaginaryValue)) / 2.0 << endl; } int main() { double n = 50; findTwoNumbersWithSameSumAndProduc(n); return 0; }

## Output

If you execute the above program, then you will get the following result.

48.9792 1.02084

## Conclusion

If you have any queries in the tutorial, mention them in the comment section.

- Related Articles
- C++ program to find two numbers with sum and product both same as N
- Find two numbers with sum and product both same as N in C++
- Take two numbers m and n & return two numbers whose sum is n and product m in JavaScript
- C++ program to find range whose sum is same as n
- Find two distinct prime numbers with given product in C++ Program
- Find two numbers whose product is $-24$ and sum is 2.
- Find two numbers whose sum is 27 and product is 182.
- C++ program to find two numbers from two arrays whose sum is not present in both arrays
- Find four factors of N with maximum product and sum equal to N - Set-2 in Python Program
- Program to find tuple with same product in Python
- Find N integers with given difference between product and sum in C++
- The product of two numbers is 24 and their sum is 14. Find the numbers.
- The sum of two numbers is 48 and their product is 432. Find the numbers.
- All possible binary numbers of length n with equal sum in both halves?
- Find four factors of N with maximum product and sum equal to N in C++

Advertisements