- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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

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; }

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

48.9792 1.02084

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

- Related Questions & Answers
- 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++
- C++ program to find range whose sum is same as n
- Take two numbers m and n & return two numbers whose sum is n and product m in JavaScript
- Find two distinct prime numbers with given product in C++ Program
- C++ program to find two numbers from two arrays whose sum is not present in both arrays
- Find N integers with given difference between product and sum in C++
- C++ find four factors of N with maximum product and sum equal to N .
- C program to find sum and difference of two numbers
- Find four factors of N with maximum product and sum equal to N in C++
- Find two distinct prime numbers with given product in C++
- Find if n can be written as product of k numbers in C++
- Maximum sum of distinct numbers with LCM as N in C++
- Find four factors of N with maximum product and sum equal to N - Set-2 in C++
- Program to find tuple with same product in Python

Advertisements