I made a quadratic equation calculator but it doesn't work. Any ideas?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace quadratic
{
class quadratic
{
static void Main(string[] args)
{
int a, b, c;
double x1, x2;
Console.Write("Please enter the value of A: ");
a = Convert.ToInt32(Console.ReadLine());
Console.WriteLine();
Console.Write("Please enter the value of B: ");
b = Convert.ToInt32(Console.ReadLine());
Console.WriteLine();
Console.Write("Please enter the value of C: ");
c = Convert.ToInt32(Console.ReadLine());
Console.WriteLine();
x1 = (-a + Math.Sqrt(b * b - 4 * a * c))/(2 * a);
x2 = (-a - Math.Sqrt(b * b - 4 * a * c))/(2 * a);
Console.WriteLine(x1);
Console.WriteLine(x2);
}
}
}
Loading
VulpesPosted Sep 12, 2012, 1:58 PM
x1 = (-b + Math.Sqrt(b * b - 4 * a * c))/(2 * a);
x2 = (-b - Math.Sqrt(b * b - 4 * a * c))/(2 * a);
Notice also that you'll get an exception if a == 0 or if the discriminant (b^2 - 4ac) is negative when the roots will be complex numbers.
For a more complete solution, check out my article on the subject:
http://www.c-sharpcorner.com/UploadFile/b942f9/using-the-complex-type-to-solve-quadratic-equations/