Check if roots of a Quadratic Equation are numerically equal but opposite in sign or not

Last Updated : 23 Aug, 2021

Given the coefficients (constants) of a quadratic equation ax^{2} + bx + c=0    , i.e. a, b, and c; the task is to check whether roots of the equation represented by these constants are numerically equal but opposite in sign or not.
Examples: 
 

Input: a = 2, b = 0, c = -1 
Output: Yes 
Explanation: 
The given quadratic equation is 2x^{2}-2=0    
Its roots are (1, -1) which are numerically equal but opposite in sign
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 numerically equal and opposite in sign 
 


 


Approach: 
To check whether roots are numerically equal but opposite in sign or not: 
 

Quadratic Equation: ax^{2} + bx + c = 0    
Let the roots be \alpha    and \beta    
Sum of roots = \alpha + \beta    \frac{-b}{a}    
Since roots are opposite in sign only, therefore \alpha = -\beta    
Therefore, 
-\beta + \beta = \frac{-b}{a}    
0 = \frac{-b}{a}    
b = 0    , i.e, coefficient of x is 0. 
 


Hence we have to only check if b is 0 or not, for the roots to be numerically equal but opposite in sign.
Below is the implementation of the above approach:
 

C++
// C++ program to check if roots
// of a Quadratic Equation are
// numerically equal but opposite
// in sign or not

#include <iostream>
using namespace std;

// Function to find the required answer
void checkSolution(int a, int b, int c)
{
    if (b == 0)
        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
// numerically equal but opposite
// in sign or not
import java.util.*;
class GFG{

// Function to find the required answer
static void checkSolution(int a, int b, int c)
{
    if (b == 0)
        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 Akanksha_Rai
Python3
# Python3 program to check if roots 
# of a quadratic equation are 
# numerically equal but opposite 
# in sign or not 

# Function to find the required answer 
def checkSolution(a, b, c): 

    if b == 0: 
        print("Yes") 
    else:
        print("No") 

# Driver code 
a = 2
b = 0
c = 2

checkSolution(a, b, c)
    
# This code is contributed by divyamohan123
C#
// C# program to check if roots
// of a Quadratic Equation are
// numerically equal but opposite
// in sign or not
using System;
class GFG{

// Function to find the required answer
static void checkSolution(int a, int b, int c)
{
    if (b == 0)
        Console.Write("Yes");
    else
        Console.Write("No");
}

// Driver code
public static void Main()
{
    int a = 2, b = 0, c = 2;

    checkSolution(a, b, c);
}
}

// This code is contributed by Akanksha_Rai
JavaScript
<script>

// Javascript program to check if roots
// of a Quadratic Equation are
// numerically equal but opposite
// in sign or not

// Function to find the required answer
function checkSolution(a, b, c)
{
    if (b == 0)
        document.write("Yes");
    else
        document.write("No");
}

// Driver code
a = 2, b = 0, c = 2;
checkSolution(a, b, c);
 
</script>

Output: 
Yes

 

Time Complexity: O(1)

Auxiliary Space: O(1)

Comment