求一元二次方程
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
double a, b, c,X1,X2; //定義變量
Console.WriteLine("--------求一元二次方程:ax^2+bx+c=0 --------");
Console.WriteLine("請輸入一個值給a:");
a = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("請輸入一個值給b:");
b = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("請輸入一個值給c:");
c = Convert.ToDouble(Console.ReadLine());
X1=(-b+Math.Sqrt(b*b-4*a*c))/2*a;
X2 = (-b - Math.Sqrt(b * b - 4 * a * c)) / 2 * a;
if (b * b - 4 * a * c < 0) Console.WriteLine("此方程無實數解!");
else if (b * b - 4 * a * c == 0) Console.WriteLine("只有一個解!X1=X2={0}",X2=X1);
else
Console.WriteLine("X1的值為:{0}\tX的值為:{1}", X2, X2);
Console.ReadKey();
}
}
}