Wednesday, September 25, 2024
Google search engine
HomeData Modelling & AICheck if roots of a Quadratic Equation are reciprocal of each other...

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

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. 

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)

Using Quadratic Formula in python:

We can find the roots of a quadratic equation using the quadratic formula:
x = (-b ± sqrt(b^2 – 4ac)) / 2a
If the roots are reciprocal of each other, then one of the roots is the reciprocal of the other:
x1 * x2 = 1
x2 = 1 / x1
Substituting x2 in terms of x1 in the above quadratic formula:
x1 = (-b ± sqrt(b^2 – 4ac)) / 2a
1 / x1 = (-b ? sqrt(b^2 – 4ac)) / 2a

So, we can find the roots using the quadratic formula and check if they satisfy the above condition to be reciprocal of each other.

  • Calculate the discriminant of the quadratic equation using the formula discriminant = B**2 – 4*A*C.
  • If the discriminant is negative, return False as the quadratic equation has no real roots.
  • Calculate the roots of the quadratic equation using the formula root1 = (-B + math.sqrt(discriminant)) / (2*A) and root2 = (-B – math.sqrt(discriminant)) / (2*A).
  • Check if the product of the roots root1 * root2 is equal to 1.
  • If the product is equal to 1, return True as the roots are reciprocal of each other. Otherwise, return False.

C++




#include <iostream>
#include <cmath>
 
// Function to check if roots are reciprocal of each other
bool areRootsReciprocal(double A, double B, double C) {
    // Calculate the discriminant
    double discriminant = B * B - 4 * A * C;
 
    // If discriminant is negative, roots are imaginary
    if (discriminant < 0) {
        return false;
    }
 
    // Calculate the roots
    double root1 = (-B + sqrt(discriminant)) / (2 * A);
    double root2 = (-B - sqrt(discriminant)) / (2 * A);
 
    // Check if the product of roots is equal to 1
    return root1 * root2 == 1;
}
 
int main() {
    // Example inputs
    double A1 = 2, B1 = -5, C1 = 2;
    double A2 = 1, B2 = -5, C2 = 6;
 
    // Check if roots are reciprocal for the first set of coefficients
    if (areRootsReciprocal(A1, B1, C1)) {
        std::cout << "Yes" << std::endl;
    } else {
        std::cout << "No" << std::endl;
    }
 
    // Check if roots are reciprocal for the second set of coefficients
    if (areRootsReciprocal(A2, B2, C2)) {
        std::cout << "Yes" << std::endl;
    } else {
        std::cout << "No" << std::endl;
    }
 
    return 0;
}


Python3




import math
 
def are_roots_reciprocal(A, B, C):
    discriminant = B**2 - 4*A*C
    if discriminant < 0:
        return False
    root1 = (-B + math.sqrt(discriminant)) / (2*A)
    root2 = (-B - math.sqrt(discriminant)) / (2*A)
    return root1 * root2 == 1
 
# Example inputs
A1, B1, C1 = 2, -5, 2
A2, B2, C2 = 1, -5, 6
 
# Check if roots are reciprocal of each other
if are_roots_reciprocal(A1, B1, C1):
    print("Yes")
else:
    print("No")
 
if are_roots_reciprocal(A2, B2, C2):
    print("Yes")
else:
    print("No")


Javascript




function areRootsReciprocal(A, B, C) {
    let discriminant = B ** 2 - 4 * A * C;
    if (discriminant < 0) {
        return false;
    }
    let root1 = (-B + Math.sqrt(discriminant)) / (2 * A);
    let root2 = (-B - Math.sqrt(discriminant)) / (2 * A);
    return root1 * root2 === 1;
}
 
// Example inputs
let A1 = 2, B1 = -5, C1 = 2;
let A2 = 1, B2 = -5, C2 = 6;
 
// Check if roots are reciprocal of each other
if (areRootsReciprocal(A1, B1, C1)) {
    console.log("Yes");
} else {
    console.log("No");
}
 
if (areRootsReciprocal(A2, B2, C2)) {
    console.log("Yes");
} else {
    console.log("No");
}


Output

Yes
No




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

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments