Open In App

Check whether one root of the Quadratic Equation is twice of other or not

Last Updated : 23 Jun, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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 one root of the equation represented by these constants is twice of other or not.
Examples: 
 

Input: A = 1, B = -3, C = 2 
Output: Yes 
Explanation: 
The given quadratic equation is x^{2} - 3x + 2 = 0
Its roots are (1, 2). 2 = 2 * 1
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). 3 \ne 2 * 2   or 2 \ne 2 * 3
 


 


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 twice of the other or not by:
 

  1. The sum of roots = \alpha   2*\alpha   = 3\alpha   . This value is equal to: 
     

\frac{-b}{a}
 

  1. Similarly, the product of the roots = \alpha   2*\alpha   = 2\alpha^2   . This value is equal to: 
     

\frac{c}{a}
 

  1. We can solve the above two equations 3 * \alpha = \frac{-b}{a}   and 2 * \alpha^2 = \frac{c}{a}   to get the condition: 
     

2b^2 = 9 * a * c
 

  1. Therefore, inorder for the first assumption of the roots to hold true, the above condition needs to hold true. Hence, we simply check if the above condition is true or not for the given coefficients.


Below is the implementation of the above approach: 
 

C++

// C++ program to check if one root
// of a Quadratic Equation is
// twice of other or not
 
#include <iostream>
using namespace std;
 
// Function to find the required answer
void checkSolution(int a, int b, int c)
{
    if (2 * b * b == 9 * a * c)
        cout << "Yes";
    else
        cout << "No";
}
 
// Driver code
int main()
{
    int a = 1, b = 3, c = 2;
 
    checkSolution(a, b, c);
 
    return 0;
}

                    

Java

// Java program to check if one root
// of a quadratic equation is
// twice of other or not
class GFG{
 
// Function to find the required answer
static void checkSolution(int a, int b, int c)
{
    if (2 * b * b == 9 * a * c)
        System.out.print("Yes");
    else
        System.out.print("No");
}
 
// Driver Code
public static void main(String[] args)
{
    int a = 1, b = 3, c = 2;
 
    checkSolution(a, b, c);
}
}
 
// This code is contributed by shubham

                    

Python3

# Python3 program to check if one root
# of a Quadratic Equation is
# twice of other or not
 
# Function to find the required answer
def checkSolution(a, b, c):
 
    if (2 * b * b == 9 * a * c):
        print("Yes");
    else:
        print("No");
 
# Driver code
a = 1; b = 3; c = 2;
checkSolution(a, b, c);
 
# This code is contributed by Code_Mech

                    

C#

// C# program to check if one root
// of a quadratic equation is
// twice of other or not
using System;
class GFG{
 
// Function to find the required answer
static void checkSolution(int a, int b, int c)
{
    if (2 * b * b == 9 * a * c)
        Console.WriteLine("Yes");
    else
        Console.WriteLine("No");
}
 
// Driver Code
public static void Main()
{
    int a = 1, b = 3, c = 2;
 
    checkSolution(a, b, c);
}
}
 
// This code is contributed by shivanisinghss2110

                    

Javascript

<script>
 
    // Javascript program to check if one root
    // of a Quadratic Equation is
    // twice of other or not 
     
    // Function to find the required answer
    function checkSolution(a, b, c)
    {
        if (2 * b * b == 9 * a * c)
            document.write("Yes");
        else
            document.write("No");
    }
     
    let a = 1, b = 3, c = 2;
   
    checkSolution(a, b, c);
 
 
</script>

                    

Output: 
Yes

 

Time Complexity: O(1)

Auxiliary Space: O(1)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads