Find the quadratic equation from the given roots - GeeksforGeeks

10 Mar, 2022
Given the roots of a quadratic equation A and B, the task is to find the equation.Note: The given roots are integral.
Examples: 
Input: A = 2, B = 3 Output: x^2 – (5x) + (6) = 0 x2 – 5x + 6 = 0 x2 -3x -2x + 6 = 0 x(x – 3) – 2(x – 3) = 0 (x – 3) (x – 2) = 0 x = 2, 3
Input: A = 5, B = 10 Output: x^2 – (15x) + (50) = 0 
Approach: If the roots of a quadratic equation ax2 + bx + c = 0 are A and B then it known that A + B = – b / a and A * B = c * a. Now, ax2 + bx + c = 0 can be written as x2 + (b / a)x + (c / a) = 0 (Since, a != 0) x2 – (A + B)x + (A * B) = 0, [Since, A + B = -b * a and A * B = c * a] i.e. x2 – (Sum of the roots)x + Product of the roots = 0
Below is the implementation of the above approach: 
C++
Java
Python3
C#
Javascript
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std; // Function to find the quadratic// equation whose roots are a and bvoid findEquation(int a, int b){    int sum = (a + b);    int product = (a * b);    cout << "x^2 - (" << sum << "x) + ("         << product << ") = 0";} // Driver codeint main(){    int a = 2, b = 3;     findEquation(a, b);     return 0;}
// Java implementation of the above approachclass GFG{         // Function to find the quadratic    // equation whose roots are a and b    static void findEquation(int a, int b)    {        int sum = (a + b);        int product = (a * b);        System.out.println("x^2 - (" + sum +                           "x) + (" + product + ") = 0");    }         // Driver code    public static void main(String args[])    {        int a = 2, b = 3;             findEquation(a, b);    }} // This code is contributed by AnkitRai01
# Python3 implementation of the approach # Function to find the quadratic# equation whose roots are a and bdef findEquation(a, b):    summ = (a + b)    product = (a * b)    print("x^2 - (", summ,          "x) + (", product, ") = 0") # Driver codea = 2b = 3 findEquation(a, b) # This code is contributed by Mohit Kumar
// C# implementation of the above approachusing System;class GFG{         // Function to find the quadratic    // equation whose roots are a and b    static void findEquation(int a, int b)    {        int sum = (a + b);        int product = (a * b);        Console.WriteLine("x^2 - (" + sum +                          "x) + (" + product + ") = 0");    }         // Driver code    public static void Main()    {        int a = 2, b = 3;             findEquation(a, b);    }} // This code is contributed by CodeMech.
<script> // Javascript implementation of the above approach // Function to find the quadratic// equation whose roots are a and bfunction findEquation(a, b){    var sum = (a + b);    var product = (a * b);    document.write("x^2 - (" + sum +                    "x) + (" + product +                    ") = 0");} // Driver Codevar a = 2, b = 3; findEquation(a, b); // This code is contributed by Ankita saini     </script>
x^2 - (5x) + (6) = 0
 
Time Complexity: O(1)
Auxiliary Space: O(1)
mohit kumar 29
ankthon
Code_Mech
imaryan20
ankita_saini
subham348
series
Mathematical
Mathematical
series
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Algorithm to solve Rubik's Cube
Program to print prime numbers from 1 to N.
Program to multiply two matrices
Fizz Buzz Implementation
Modular multiplicative inverse
Complexity Analysis of Binary Search
Check if a number is Palindrome
Find Union and Intersection of two unsorted arrays
Count ways to reach the n'th stair
Find first and last digits of a number