 Open in App
Not now

# Find two numbers with difference and division both same as N

• Last Updated : 28 Feb, 2022

Given an integer N, the task is to find two numbers a and b such that a / b = N and a – b = N. Print “No” if no such numbers are possible.
Examples:

Input: N = 6
Output:
a = 7.2
b = 1.2
Explanation:
For the given two numbers a and b, a/b = 6 = N and a-b = 6 = N

Input: N = 1
Output: No
Explanation:
There are no values of a and b that satisfy the condition.

Approach: To solve the problem observe the equations derived below: On solving above equations simultaneously, we get:  Since the denominator is N – 1, so the answer will not be possible when N = 1. For all other cases, the answer is possible. Hence find the values of a and b respectively.
Below is the implementation of the above approach:

## C++

 // C++ program for the above approach#include using namespace std; // Function to find two numbers with// difference and division both as Nvoid findAandB(double N){    // Condition if the answer    // is not possible     if (N == 1) {        cout << "No";        return;    }     // Calculate a and b    double a = N * N / (N - 1);    double b = a / N;     // Print the values    cout << "a = " << a << endl;    cout << "b = " << b << endl;} // Driver Codeint main(){    // Given N    double N = 6;     // Function Call    findAandB(N);    return 0;}

## Java

 // Java program for the above approachclass GFG{ // Function to find two numbers with// difference and division both as Nstatic void findAandB(double N){         // Condition if the answer    // is not possible    if (N == 1)    {        System.out.print("No");        return;    }     // Calculate a and b    double a = N * N / (N - 1);    double b = a / N;     // Print the values    System.out.print("a = " + a + "\n");    System.out.print("b = " + b + "\n");} // Driver Codepublic static void main(String[] args){         // Given N    double N = 6;     // Function call    findAandB(N);}} // This code is contributed by Rajput-Ji

## Python3

 # Python3 program for the above approach # Function to find two numbers with# difference and division both as Ndef findAandB(N):         # Condition if the answer    # is not possible    if (N == 1):        print("No")        return         # Calculate a and b    a = N * N / (N - 1)    b = a / N     # Print the values    print("a = ", a)    print("b = ", b) # Driver Code # Given NN = 6 # Function callfindAandB(N) # This code is contributed by sanjoy_62

## C#

 // C# program for the above approachusing System; class GFG{ // Function to find two numbers with// difference and division both as Nstatic void findAandB(double N){     // Condition if the answer    // is not possible    if (N == 1)    {        Console.Write("No");        return;    }     // Calculate a and b    double a = N * N / (N - 1);    double b = a / N;     // Print the values    Console.Write("a = " + a + "\n");    Console.Write("b = " + b + "\n");} // Driver Codepublic static void Main(String[] args){     // Given N    double N = 6;     // Function call    findAandB(N);}} // This code is contributed by amal kumar choubey

## Javascript

 

Output:

a = 7.2
b = 1.2

Time Complexity: O(1)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up