Find two numbers with difference and division both same as N

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to find two numbers with
// difference and division both as N
void 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 Code
int main()
{
    // Given N
    double N = 6;
  
    // Function Call
    findAandB(N);
    return 0;
}
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program for the above approach
class GFG{
  
// Function to find two numbers with
// difference and division both as N
static 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 Code
public static void main(String[] args)
{
      
    // Given N
    double N = 6;
  
    // Function call
    findAandB(N);
}
}
  
// This code is contributed by Rajput-Ji
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program for the above approach
  
# Function to find two numbers with
# difference and division both as N
def 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 N
N = 6
  
# Function call
findAandB(N)
  
# This code is contributed by sanjoy_62
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program for the above approach
using System;
  
class GFG{
  
// Function to find two numbers with
// difference and division both as N
static 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 Code
public static void Main(String[] args)
{
  
    // Given N
    double N = 6;
  
    // Function call
    findAandB(N);
}
}
  
// This code is contributed by amal kumar choubey
chevron_right

Output: 
a = 7.2
b = 1.2

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




Recommended Posts:


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Article Tags :