The Standard Form of a Quadratic Equation looks like this:
ax2 + bx + c = 0
The term b2-4ac
is known as the discriminant of a quadratic equation.
The discriminant tells the nature of the roots.
If discriminant is greater than 0, the roots are real and different.
If discriminant is equal to 0, the roots are real and equal.
If discriminant is less than 0, the roots are complex and different.
C# Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp2 { class Program { public static void SolveQuadratic(double a, double b, double c) { double sqrtpart = b * b - 4 * a * c; double x, x1, x2, img; if (sqrtpart > 0) { x1 = (-b + System.Math.Sqrt(sqrtpart)) / (2 * a); x2 = (-b - System.Math.Sqrt(sqrtpart)) / (2 * a); Console.WriteLine("Two Real Solutions: {0,8:f4} or {1,8:f4}", x1, x2); } else if (sqrtpart < 0) { sqrtpart = -sqrtpart; x = -b / (2 * a); img = System.Math.Sqrt(sqrtpart) / (2 * a); Console.WriteLine("Two Imaginary Solutions: {0,8:f4} + {1,8:f4} i or {2,8:f4} + {3,8:f4} i", x, img, x, img); } else { x = (-b + System.Math.Sqrt(sqrtpart)) / (2 * a); Console.WriteLine("One Real Solution: {0,8:f4}", x); } } static void Main(string[] args) { int a, b, c; Console.Write("a : "); a = Int32.Parse(Console.ReadLine()); Console.Write("b : "); b = Int32.Parse(Console.ReadLine()); Console.Write("c : "); c = Int32.Parse(Console.ReadLine()); SolveQuadratic(a, b, c); Console.ReadKey(); } } } |
Output: