📜  从给定的根形成三次方程

📅  最后修改于: 2021-04-21 22:14:55             🧑  作者: Mango

给定三次方程式ABC的根,任务是从给定的根形成三次方程式。
注意:给定的根是整数。
例子:

方法:令三次方程的根( ax 3 + bx 2 + cx + d = 0 )为A,B和C。则给定的三次方程可表示为:

因此,使用上述关系式找到XYZ的值,并形成所需的三次方程。
下面是上述方法的实现:

C++
// C++ program for the approach
 
#include 
using namespace std;
 
// Function to find the cubic
// equation whose roots are a, b and c
void findEquation(int a, int b, int c)
{
    // Find the value of coefficient
    int X = (a + b + c);
    int Y = (a * b) + (b * c) + (c * a);
    int Z = a * b * c;
 
    // Print the equation as per the
    // above coefficients
    cout << "x^3 - " << X << "x^2 + "
         << Y << "x - " << Z << " = 0";
}
 
// Driver Code
int main()
{
    int a = 5, b = 2, c = 3;
 
    // Function Call
    findEquation(a, b, c);
    return 0;
}


Java
// Java program for the approach
 
class GFG{
 
// Function to find the cubic equation
// whose roots are a, b and c
static void findEquation(int a, int b, int c)
{
    // Find the value of coefficient
    int X = (a + b + c);
    int Y = (a * b) + (b * c) + (c * a);
    int Z = a * b * c;
 
    // Print the equation as per the
    // above coefficients
    System.out.print("x^3 - " + X+ "x^2 + "
                  + Y+ "x - " + Z+ " = 0");
}
 
// Driver Code
public static void main(String[] args)
{
    int a = 5, b = 2, c = 3;
 
    // Function Call
    findEquation(a, b, c);
}
}
 
// This code contributed by PrinciRaj1992


Python3
# Python3 program for the approach
 
# Function to find the cubic equation
# whose roots are a, b and c
def findEquation(a, b, c):
     
    # Find the value of coefficient
    X = (a + b + c);
    Y = (a * b) + (b * c) + (c * a);
    Z = (a * b * c);
 
    # Print the equation as per the
    # above coefficients
    print("x^3 - " , X ,
          "x^2 + " ,Y ,
          "x - " , Z , " = 0");
 
# Driver Code
if __name__ == '__main__':
     
    a = 5;
    b = 2;
    c = 3;
 
    # Function Call
    findEquation(a, b, c);
 
# This code is contributed by sapnasingh4991


C#
// C# program for the approach
using System;
 
class GFG{
 
// Function to find the cubic equation
// whose roots are a, b and c
static void findEquation(int a, int b, int c)
{
     
    // Find the value of coefficient
    int X = (a + b + c);
    int Y = (a * b) + (b * c) + (c * a);
    int Z = a * b * c;
 
    // Print the equation as per the
    // above coefficients
    Console.Write("x^3 - " + X +
                  "x^2 + " + Y +
                    "x - " + Z + " = 0");
}
 
// Driver Code
public static void Main()
{
    int a = 5, b = 2, c = 3;
 
    // Function Call
    findEquation(a, b, c);
}
}
 
// This code is contributed by shivanisinghss2110


Javascript


输出:
x^3 - 10x^2 + 31x - 30 = 0