Roots of the quadratic equation when a + b + c = 0 without using Shridharacharya formula - GeeksforGeeks

26 Mar, 2021
Given three integers a, b and c such that a + b + c = 0. The task is to find the roots of a quadratic equation ax2 + bx + c = 0.Examples:  
Input: a = 1, b = 2, c = -3 Output: 1, -3Input: a = -5, b = 3, c = 2 Output: 1, -2.5  
 
Approach: When a + b + c = 0 then the roots of the equation ax2 + bx + c = 0 are always 1 and c / a. For example,  
Take a = 3, b = 2 and c = -5 such that a + b + c = 0 Now, the equation will be 3x2 + 2x – 5 = 0 Solving for x, 3x2 + 5x – 3x – 5 = 0 x * (3x + 5) -1 * (3x + 5) = 0 (x – 1) * (3x + 5) = 0 x = 1, x = (-5 / 3) = (c / a)  
Below is the implementation of the above approach:  
C++
Java
Python3
C#
PHP
Javascript
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std; // Function to print the roots of the// quadratic equation when a + b + c = 0void printRoots(long a, long b, long c){    cout << 1 << ", " << c / (a * 1.0);} // Driver codeint main(){    long a = 2;    long b = 3;    long c = -5;    printRoots(a, b, c);     return 0;}
// Java implementation of the approachclass GFG{         // Function to print the roots of the    // quadratic equation when a + b + c = 0    static void printRoots(long a, long b, long c)    {        System.out.println(1 + ", " + c / (a * 1.0));    }         // Driver Code    public static void main (String[] args)    {        long a = 2;        long b = 3;        long c = -5;        printRoots(a, b, c);    }} // This code is contributed by// sanjeev2552
# Python3 implementation of the approach # Function to print the roots of the# quadratic equation when a + b + c = 0def printRoots(a, b, c):    print(1, ",", c / (a * 1.0)) # Driver codea = 2b = 3c = -5printRoots(a, b, c) # This code is contributed by Mohit Kumar
// C# implementation of the approachusing System; class GFG{ // Function to print the roots of the// quadratic equation when a + b + c = 0static void printRoots(long a, long b, long c){    Console.WriteLine("1, " + c / (a * 1.0));} // Driver codepublic static void Main(){    long a = 2;    long b = 3;    long c = -5;    printRoots(a, b, c);}} // This code is contributed by Nidhi
<?php// PHP implementation of the approach // Function to print the roots of the// quadratic equation when a + b + c = 0function printRoots($a, $b, $c){    echo "1";    echo ", ";    echo $c / ($a * 1.0);} // Driver code$a = 2;$b = 3;$c = -5;printRoots($a, $b, $c); // This code is contributed by Naman_Garg.?>
<script>// Javascript implementation of the approach // Function to print the roots of the// quadratic equation when a + b + c = 0function printRoots(a, b, c){    document.write(1 + ", " + c / (a * 1.0));} // Driver codevar a = 2;var b = 3;var c = -5;printRoots(a, b, c); // This code is contributed by noob2000.</script>
1, -2.5
 
Time Complexity: O(1) 
mohit kumar 29
nidhi_biet
Naman_Garg
sanjeev2552
noob2000
maths-polynomial
Mathematical
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Program to print prime numbers from 1 to N.
Modular multiplicative inverse
Fizz Buzz Implementation
Check if a number is Palindrome
Generate all permutation of a set in Python
Program to multiply two matrices
Segment Tree | Set 1 (Sum of given range)
Merge two sorted arrays with O(1) extra space
Count ways to reach the n'th stair
How to check if a given point lies inside or outside a polygon?