Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Check if roots of a Quadratic Equation are reciprocal of each other or not

  • Last Updated : 24 Mar, 2021

Given three numbers A, B, C which represents the coefficients(constants) of a quadratic equation Ax^{2} + Bx + C = 0    , the task is to check whether the roots of the equation represented by these constants are reciprocal of each other or not.
Examples: 

Input: A = 2, B = -5, C = 2 
Output: Yes 
Explanation: 
The given quadratic equation is 2x^{2} - 2 = 0
Its roots are (1, 1/1) which are reciprocal of each other.
Input: A = 1, B = -5, C = 6 
Output: No 
Explanation: 
The given quadratic equation is x^{2} - 5x + 6 = 0
Its roots are (2, 3) which are not reciprocal of each other. 

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

Approach: The idea is to use the concept of quadratic roots to solve the problem. We can formulate the condition required to check whether one root is the reciprocal of the other or not by:  

  1. Let the roots of the equation be \alpha    and \frac{1}{\alpha}    .
  2. The product of the roots of the above equation is given by \alpha    \frac{1}{\alpha}    .
  3. It is known that the product of the roots is C/A. Therefore, the required condition is C = A.

Below is the implementation of the above approach:  

C++




// C++ program to check if roots
// of a Quadratic Equation are
// reciprocal of each other or not
 
#include <iostream>
using namespace std;
 
// Function to check if the roots
// of a quadratic equation are
// reciprocal of each other or not
void checkSolution(int a, int b, int c)
{
    if (a == c)
        cout << "Yes";
    else
        cout << "No";
}
 
// Driver code
int main()
{
    int a = 2, b = 0, c = 2;
 
    checkSolution(a, b, c);
 
    return 0;
}


Java




// Java program to check if roots
// of a quadratic equation are
// reciprocal of each other or not
class GFG{
 
// Function to check if the roots 
// of a quadratic equation are
// reciprocal of each other or not
static void checkSolution(int a, int b, int c)
{
    if (a == c)
        System.out.print("Yes");
    else
        System.out.print("No");
}
 
// Driver code
public static void main(String[] args)
{
    int a = 2, b = 0, c = 2;
 
    checkSolution(a, b, c);
}
}
 
// This code is contributed by shubham


Python3




# Python3 program to check if roots
# of a Quadratic Equation are
# reciprocal of each other or not
 
# Function to check if the roots
# of a quadratic equation are
# reciprocal of each other or not
def checkSolution(a, b, c):
 
    if (a == c):
        print("Yes");
    else:
        print("No");
 
# Driver code
a = 2; b = 0; c = 2;
checkSolution(a, b, c);
 
# This code is contributed by Code_Mech


C#




// C# program to check if roots
// of a quadratic equation are
// reciprocal of each other or not
using System;
class GFG{
 
// Function to check if the roots
// of a quadratic equation are
// reciprocal of each other or not
static void checkSolution(int a, int b, int c)
{
    if (a == c)
        Console.WriteLine("Yes");
    else
        Console.WriteLine("No");
}
 
// Driver code
public static void Main()
{
    int a = 2, b = 0, c = 2;
 
    checkSolution(a, b, c);
}
}
 
// This code is contributed by shivanisinghss2110


Javascript




<script>
 
    // Javascript program to check if roots
    // of a Quadratic Equation are
    // reciprocal of each other or not
     
    // Function to check if the roots
    // of a quadratic equation are
    // reciprocal of each other or not
    function checkSolution(a, b, c)
    {
        if (a == c)
            document.write("Yes");
        else
            document.write("No");
    }
 
    let a = 2, b = 0, c = 2;
  
    checkSolution(a, b, c);
     
</script>


Output: 

Yes

 

Time Complexity: O(1)

Auxiliary Space: O(1)




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!