Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class QuadraticEquation
- {
- static void Main(string[] args)
- {
- double a, b, c;
- Console.WriteLine("ax^2 + bx + c = 0");
- Console.WriteLine("");
- Console.Write("Enter a: ");
- bool aIsValid = double.TryParse(Console.ReadLine(), out a);
- Console.Write("Enter b: ");
- bool bIsValid = double.TryParse(Console.ReadLine(), out b);
- Console.Write("Enter c: ");
- bool cIsValid = double.TryParse(Console.ReadLine(), out c);
- if (aIsValid && bIsValid && cIsValid)
- {
- //D = b^2 - 4*a*c
- double d = b * b - (4 * a * c);
- double x1 = (-b + Math.Sqrt(d)) / (2 * a);
- double x2 = (-b - Math.Sqrt(d)) / (2 * a);
- if (double.IsNaN(x1) && double.IsNaN(x2))
- {
- Console.WriteLine("No real roots.");
- }
- else
- {
- Console.WriteLine("{0} {1}", x1, x2);
- }
- }
- else
- {
- Console.WriteLine("Please enter valid data.");
- Console.Beep();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement