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

C++Server Side ProgrammingProgramming

#### C in Depth: The Complete C Programming Guide for Beginners

45 Lectures 4.5 hours

#### Practical C++: Learn C++ Basics Step by Step

Most Popular

50 Lectures 4.5 hours

#### Master C and Embedded C Programming- Learn as you go

66 Lectures 5.5 hours

In this tutorial, we will be discussing a program to find two numbers with sum and product both same as N.

For this we will be provided with an integer value. Our task is to find two other integer values whose product and sum is equal to the given value.

## Example

Live Demo

#include <bits/stdc++.h>
using namespace std;
//finding a and b such that
//a*b=N and a+b=N
void calculateTwoValues(double N) {
double val = N * N - 4.0 * N;
if (val < 0) {
cout << "NO";
return;
}
double a = (N + sqrt(val)) / 2.0;
double b = (N - sqrt(val)) / 2.0;
cout << "Value of A:" << a << endl;
cout << "Value of B:" << b << endl;
}
int main() {
double N = 57.0;
calculateTwoValues(N);
return 0;
}

## Output

Value of A:55.9818
Value of B:1.01819
Updated on 19-Aug-2020 10:42:22