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 = NInput: 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 <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; } |
Java
// 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 |
Python3
# 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 |
C#
// 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 |
Javascript
<script> // Javascript program for the above approach // Function to find two numbers with // difference and division both as N function findAandB( N) { // Condition if the answer // is not possible if (N == 1) { document.write( "No" ); return ; } // Calculate a and b let a = N * N / (N - 1); let b = a / N; // Print the values document.write( "a = " + a + "<br/>" ); document.write( "b = " + b + "<br/>" ); } // Driver Code // Given N let N = 6; // Function call findAandB(N); // This code contributed by aashish1995 </script> |
Output:
a = 7.2 b = 1.2
Time Complexity: O(1)
Auxiliary Space: O(1)
Please Login to comment...