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:
C++
#include <bits/stdc++.h>
using namespace std;
void findAandB( double N)
{
if (N == 1) {
cout << "No" ;
return ;
}
double a = N * N / (N - 1);
double b = a / N;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
}
int main()
{
double N = 6;
findAandB(N);
return 0;
}
|
Java
class GFG{
static void findAandB( double N)
{
if (N == 1 )
{
System.out.print( "No" );
return ;
}
double a = N * N / (N - 1 );
double b = a / N;
System.out.print( "a = " + a + "\n" );
System.out.print( "b = " + b + "\n" );
}
public static void main(String[] args)
{
double N = 6 ;
findAandB(N);
}
}
|
Python3
def findAandB(N):
if (N = = 1 ):
print ( "No" )
return
a = N * N / (N - 1 )
b = a / N
print ( "a = " , a)
print ( "b = " , b)
N = 6
findAandB(N)
|
C#
using System;
class GFG{
static void findAandB( double N)
{
if (N == 1)
{
Console.Write( "No" );
return ;
}
double a = N * N / (N - 1);
double b = a / N;
Console.Write( "a = " + a + "\n" );
Console.Write( "b = " + b + "\n" );
}
public static void Main(String[] args)
{
double N = 6;
findAandB(N);
}
}
|
Javascript
<script>
function findAandB( N) {
if (N == 1) {
document.write( "No" );
return ;
}
let a = N * N / (N - 1);
let b = a / N;
document.write( "a = " + a + "<br/>" );
document.write( "b = " + b + "<br/>" );
}
let N = 6;
findAandB(N);
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
28 Feb, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...