# Form the Cubic equation from the given roots

Given the roots of a cubic equation A, B and C, the task is to form the Cubic equation from the given roots.

Note: The given roots are integral.

Examples:

Input: A = 1, B = 2, C = 3
Output: x^3 – 6x^2 + 11x – 6 = 0
Explanation:
Since 1, 2, and 3 are roots of the cubic equations, Then equation is given by:
(x – 1)(x – 2)(x – 3) = 0
(x – 1)(x^2 – 5x + 6) = 0
x^3 – 5x^2 + 6x – x^2 + 5x – 6 = 0
x^3 – 6x^2 + 11x – 6 = 0.

Input: A = 5, B = 2, C = 3
Output: x^3 – 10x^2 + 31x – 30 = 0
Explanation:
Since 5, 2, and 3 are roots of the cubic equations, Then equation is given by:
(x – 5)(x – 2)(x – 3) = 0
(x – 5)(x^2 – 5x + 6) = 0
x^3 – 5x^2 + 6x – 5x^2 + 25x – 30 = 0
x^3 – 10x^2 + 31x – 30 = 0.

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: Let the root of the cubic equation (ax3 + bx2 + cx + d = 0) be A, B and C. Then the given cubic equation can be represents as:

ax3 + bx2 + cx + d = x3 – (A + B + C)x2 + (AB + BC +CA)x + A*B*C = 0.
Let X = (A + B + C)
Y = (AB + BC +CA)
Z = A*B*C

Therefore using the above relation find the value of X, Y, and Z and form the required cubic equation.

Below is the implementation of the above approach:

## 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 `

Output:

```x^3 - 10x^2 + 31x - 30 = 0
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.