📜  从给定的根中找出二次方程

📅  最后修改于: 2021-05-04 22:06:49             🧑  作者: Mango

给定二次方程AB的根,任务是找到方程。
注意:给定的根是整数。

例子:

方法:如果二次方程ax 2 + bx + c = 0的根AB,则它知道
A + B = – b / aA * B = c * a
现在,ax 2 + bx + c = 0可以写成
x 2 +(b / a)x +(c / a)= 0(因为a!= 0)
x 2 –(A + B)x +(A * B)= 0,[因为,A + B = -b * a和A * B = c * a]
x 2 –(根的总和)x +根的乘积= 0

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to find the quadratic
// equation whose roots are a and b
void findEquation(int a, int b)
{
    int sum = (a + b);
    int product = (a * b);
    cout << "x^2 - (" << sum << "x) + ("
         << product << ") = 0";
}
 
// Driver code
int main()
{
    int a = 2, b = 3;
 
    findEquation(a, b);
 
    return 0;
}


Java
// Java implementation of the above approach
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);
        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
# Python3 implementation of the approach
 
# Function to find the quadratic
# equation whose roots are a and b
def findEquation(a, b):
    summ = (a + b)
    product = (a * b)
    print("x^2 - (", summ,
          "x) + (", product, ") = 0")
 
# Driver code
a = 2
b = 3
 
findEquation(a, b)
 
# This code is contributed by Mohit Kumar


C#
// C# implementation of the above approach
using 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.


Javascript


输出:
x^2 - (5x) + (6) = 0