- The program below perfectly solves the Cubic Equation of the form Ax^3 + Bx^2 + Cx + D.
- The flawless code is written in .Net C#.
- The real and imaginary roots are separated by black and red colors respectively.
- Simply update the value of variables A, B, C and D to see the answers i.e. the solution of the equation.
- To find the 2 roots, solution of a Quadratic equation please visit here.
- This code is written by Code-Kings exclusively for YOU! Please like us wherever possible and show some love in return!
namespace
WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public
Form1()
        {
            InitializeComponent();
        }
        private
void CubicCalculations()
        {
            try
            {
                double
a = double.Parse(TCubA.Text);
                double
b = double.Parse(TCubB.Text);
                double
c = double.Parse(TCubC.Text);
                double
d = double.Parse(TCubD.Text);
                double
f = (((3 * c) / a) - ((b * b) / (a * a))) / 3;
                string
f2 = f.ToString();
                double
g = (((2 * (Math.Pow(b, 3))) / (Math.Pow(a, 3))) - ((9 * b * c) / (Math.Pow(a, 2))) + ((27 * d) / a)) / 27;
                string
g2 = g.ToString();
                double
h = ((Math.Pow(g, 2)) / 4) + ((Math.Pow(f, 3)) / 27);
                string
h2 = h.ToString();
                if
(h <= 0)
                {
                    double
i = (((Math.Pow(g, 2)) / 4) - h);
                    double
i2 = Math.Pow(i, 0.5);
                    string
i3 = i2.ToString();
                    double
j = (Math.Pow(i2,
0.333333333333333333333333));
                    string
j2 = j.ToString();
                    double
k = ((Math.Acos(0 - (g / (2 * i2)))));
                    string
k2 = k.ToString();
                    double
l = (j * (0 - 1));
                    string
l2 = l.ToString();
                    double
m = ((Math.Cos(k / 3)));
                    string
m2 = m.ToString();
                    double
n = ((Math.Pow(3, 0.5)) * Math.Sin(k / 3));
                    string
n2 = n.ToString();
                    double
p = ((b / (3 * a)) * (0 - 1));
                    string
p2 = p.ToString();
                    double
x = ((2 * j) * (Math.Cos(k / 3)) - (b / (3 *
a)));
                    string
x2 = x.ToString();
                    TCubFR.Text = x2;
                    double
y = (l * (m + n) + p);
                    string
y2 = y.ToString();
                    TCubSR.Text = y2;
                    double
z = (l * (m - n) + p);
                    string
z2 = z.ToString();
                    TCubTR.Text = z2;
                }
                if
(h > 0)
                {
                    double
u = 0 - g;
                    double
r = (u / 2) + (Math.Pow(h, 0.5));
                    string
r2 = r.ToString();
                    if
(r >= 0)
                    {
                        double s6 = (Math.Pow(r,
0.333333333333333333333333333));
                        double s8 = s6;
                        string s9 = s8.ToString();
                        double t8 = (u / 2) - (Math.Pow(h,
0.5));
                        string t9 = t8.ToString();
                        double help8 = (1.0 / 3);
                        string help9 = help8.ToString();
                        if (t8 < 0)
                        {
                            double v7 = (Math.Pow((0
- t8), 0.33333333333333333333));
                            double v8 = (v7);
                            string v9 = v8.ToString();
                            double x3 = (s8 - v8) - (b / (3 * a));
                            string x4 = x3.ToString();
                            TCubFR.Text = x4;
                            double y3 = ((0 - (s8 - v8)) / 2) - (b / (3 * a));
                            double y4 = ((s8 + v8) * (Math.Sqrt(3)
/ 2));
                            string y6 = y3.ToString();
                            string y7 = y4.ToString();
                            TCubSR.Text = y6 + " + " + y7 + "i";
                            double z3 = ((0 - (s8 - v8)) / 2) - (b / (3 * a));
                            double z4 = ((s8 + v8) * (Math.Sqrt(3))
/ 2);
                            string z6 = z3.ToString();
                            string z7 = z4.ToString();
                            TCubTR.Text = z6 + " - " + z7 + "i";
                        }
                        if
(t8 >= 0)
                        {
                            double v6 = (Math.Pow((0
- (0 - t8)), 0.33333333333333333333));
                            double v5 = (v6);
                            string v4 = v5.ToString();
                            double x3 = (s8 + v5) - (b / (3 * a));
                            string x4 = x3.ToString();
                            TCubFR.Text = x4;
                            double
y3 = ((0 - (s8 + v5)) / 2) - (b / (3 * a));
                            double y4 = ((s8 - v5) * (Math.Sqrt(3)
/ 2));
                            string y6 = y3.ToString();
                            string y7 = y4.ToString();
                            TCubSR.Text = y6 + " + " + y7 + "i";
                            double z3 = ((0 - (s8 + v5)) / 2) - (b / (3 * a));
                            double z4 = ((s8 - v5) * (Math.Sqrt(3))
/ 2);
                            string z6 = z3.ToString();
                            string z7 = z4.ToString();
                            TCubTR.Text = z6 + " - " + z7 + "i";
                        }
                    }
                    if
(r < 0)
                    {
                        double s3 = (Math.Pow((0 - r),
0.333333333333333333333333333));
                        double s = (0 - s3);
                        string s2 = s.ToString();
                        double t = (u / 2) - (Math.Pow(h,
0.5));
                        string t2 = t.ToString();
                        double help = (1.0 / 3);
                        string help2 = help.ToString();
                        if (t < 0)
                        {
                            double v = (Math.Pow((0
- t), 0.33333333333333333333));
                            double v2 = (v);
                            string v3 = v2.ToString();
                            double x3 = (s - v2) - (b / (3 * a));
                            string x4 = x3.ToString();
                            TCubFR.Text = x4;
                            double y3 = ((0 - (s - v2)) / 2) - (b / (3 * a));
                            double y4 = ((s + v2) * (Math.Sqrt(3)
/ 2));
                            string y6 = y3.ToString();
                            string y7 = y4.ToString();
                            TCubSR.Text = y6 + " + " + y7 + "i";
                            double z3 = ((0 - (s - v2)) / 2) - (b / (3 * a));
                            double z4 = ((s + v2) * (Math.Sqrt(3))
/ 2);
                            string z6 = z3.ToString();
                            string z7 = z4.ToString();
                            TCubTR.Text = z6 + " - " + z7 + "i";
                        }
                        if (t >= 0)
                        {
                            double v = (Math.Pow((0
- (0 - t)), 0.33333333333333333333));
                            double v2 = (v);
                            string v3 = v2.ToString();
                            double x3 = (s + v2) - (b / (3 * a));
                            string x4 = x3.ToString();
                            TCubFR.Text = x4;
                            double y3 = ((0 - (s + v2)) / 2) - (b / (3 * a));
                            double y4 = ((s - v2) * (Math.Sqrt(3)
/ 2));
                            string y6 = y3.ToString();
                            string y7 = y4.ToString();
                            TCubSR.Text = y6 + " + " + y7 + "i";
                            double z3 = ((0 - (s + v2)) / 2) - (b / (3 * a));
                            double z4 = ((s - v2) * (Math.Sqrt(3))
/ 2);
                            string z6 = z3.ToString();
                            string z7 = z4.ToString();
                            TCubTR.Text = z6 + " - " + z7 + "i";
                        }
                    }
                }
                if
(h == 0 && f == 0 && g == 0)
                {
                    double
x5 = ((Math.Pow((d / a),
0.3333333333333333333))) * (0 - 1);
                    string
x6 = x5.ToString();
                    TCubFR.Text = x6;
                    TCubSR.Text = x6;
                    TCubTR.Text = x6;
                }
            }
            catch
{ }
        }
        private
void TCubA_TextChanged(object
sender, EventArgs e)
        {
            CubicCalculations();
        }
        private
void TCubB_TextChanged(object
sender, EventArgs e)
        {
            CubicCalculations();
        }
        private
void TCubC_TextChanged(object
sender, EventArgs e)
        {
            CubicCalculations();
        }
        private
void TCubD_TextChanged(object
sender, EventArgs e)
        {
            CubicCalculations();
        }
        private
void TCubFR_TextChanged(object sender, EventArgs e)
        {
            if
(TCubFR.Text.Contains("i"))
            {
                TCubFR.ForeColor = Color.Red;
            }
            else
            {
                TCubFR.ForeColor = Color.Black;
            }
        }
        private
void TCubSR_TextChanged(object sender, EventArgs e)
        {
            if
(TCubSR.Text.Contains("i"))
            {
                TCubSR.ForeColor = Color.Red;
            }
            else
            {
                TCubSR.ForeColor = Color.Black;
            }
        }
        private
void TCubTR_TextChanged(object sender, EventArgs e)
        {
            if
(TCubTR.Text.Contains("i"))
            {
                TCubTR.ForeColor = Color.Red;
            }
            else
            {
                TCubTR.ForeColor = Color.Black;
            }
        }
    }
}

