
- 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
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 Articles
- 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
- 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
- Find two distinct prime numbers with given product in C++ Program
- C++ find four factors of N with maximum product and sum equal to N .
- Program to find tuple with same product in Python
- 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++
- 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.

Advertisements