

- 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
C++ program to find two numbers with sum and product both same as N
In this tutorial, we will be discussing a program to find two numbers (say ‘a’ and ‘b’) such that both
a+b = N and a*b = N are satisfied.
Eliminating ‘a’ from both the equations, we get a quadratic equation in ‘b’ and ‘N’ i.e
b2 - bN + N = 0
This equation will have two roots which will give us the value of both ‘a’ and ‘b’. Using the determinant method to find the roots, we get the value of ‘a’ and ‘b’ as,
$a= (N-\sqrt{N*N-4N)}/2\ b= (N+\sqrt{N*N-4N)}/2 $
Example
#include <iostream> //header file for the square root function #include <math.h> using namespace std; int main() { float N = 12,a,b; cin >> N; //using determinant method to find roots a = (N + sqrt(N*N - 4*N))/2; b = (N - sqrt(N*N - 4*N))/2; cout << "The two integers are :" << endl; cout << "a - " << a << endl; cout << "b - " << b << endl; return 0; }
Output
The two integers are : a - 10.899 b - 1.10102
- Related Questions & Answers
- Find two numbers with sum and product both same as N in C++ Program
- 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
- C++ program to find two numbers from two arrays whose sum is not present in both arrays
- C++ find four factors of N with maximum product and sum equal to N .
- C program to find sum and difference of two numbers
- Program to find tuple with same product in Python
- Find two distinct prime numbers with given product in C++ Program
- Find four factors of N with maximum product and sum equal to N - Set-2 in Python Program
- Find four factors of N with maximum product and sum equal to N in C++
- Find N integers with given difference between product and sum in C++
- All possible binary numbers of length n with equal sum in both halves?
- Python Program to Find the Product of two Numbers Using Recursion
- Java Program to Find the Product of Two Numbers Using Recursion
Advertisements