Given three integers A, B, and C representing the coefficients of a quadratic equation Ax2 + Bx + C = 0, the task is to find the quadratic equation whose roots are reciprocal to the roots of the given equation.
Examples:
Input: A = 1, B = -5, C = 6
Output: (6)x^2 +(-5)x + (1) = 0
Explanation:
The given quadratic equation x2 – 5x + 6 = 0.
Roots of the above equation are 2, 3.
Reciprocal of these roots are 1/2, 1/3.
Therefore, the quadratic equation with these reciprocal roots is 6x2 – 5x + 1 = 0.
Input: A = 1, B = -7, C = 12
Output: (12)x^2 +(-7)x + (1) = 0
Approach: The idea is to use the concept of quadratic roots to solve the problem. Follow the steps below to solve the problem:
- Consider the roots of the equation Ax2 + Bx + C = 0 to be p, q.
- The product of the roots of the above equation is given by p * q = C / A.
- The sum of the roots of the above equation is given by p + q = -B / A.
- Therefore, the reciprocals of the roots are 1/p, 1/q.
- The product of these reciprocal roots is 1/p * 1/q = A / C.
- The sum of these reciprocal roots is 1/p + 1/q = -B / C.
- If the sum and product of roots is known, the quadratic equation can be x2 – (Sum of the roots)x + (Product of the roots) = 0.
- On solving the above equation, quadratic equation becomes Cx2 + Bx + A = 0.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findEquation( int A, int B, int C)
{
cout << "(" << C << ")"
<< "x^2 +(" << B << ")x + ("
<< A << ") = 0" ;
}
int main()
{
int A = 1, B = -5, C = 6;
findEquation(A, B, C);
return 0;
}
|
Java
class GFG{
static void findEquation( int A, int B, int C)
{
System.out.print( "(" + C + ")" +
"x^2 +(" + B + ")x + (" +
A + ") = 0" );
}
public static void main(String args[])
{
int A = 1 , B = - 5 , C = 6 ;
findEquation(A, B, C);
}
}
|
Python3
def findEquation(A, B, C):
print ( "(" + str (C) + ")" +
"x^2 +(" + str (B) + ")x + (" +
str (A) + ") = 0" )
if __name__ = = "__main__" :
A = 1
B = - 5
C = 6
findEquation(A, B, C)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void findEquation( int A, int B, int C)
{
Console.Write( "(" + C + ")" +
"x^2 +(" + B + ")x + (" +
A + ") = 0" );
}
public static void Main()
{
int A = 1, B = -5, C = 6;
findEquation(A, B, C);
}
}
|
Javascript
<script>
function findEquation(A, B, C)
{
document.write( "(" + C + ")" +
"x^2 +(" + B +
")x + (" + A + ") = 0" )
}
let A = 1, B = -5, C = 6;
findEquation(A, B, C);
</script>
|
Output: (6)x^2 +(-5)x + (1) = 0
Time Complexity: O(1)
Auxiliary Space: O(1)