Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class QuadraticEquation
- {
- static void Main()
- {
- Console.WriteLine("This program solves quadratic equations of the form ax^2 + bx + c = 0");
- Console.Write("Please enter coefficient a: ");
- double a = double.Parse(Console.ReadLine());
- Console.Write("Please enter coefficient b: ");
- double b = double.Parse(Console.ReadLine());
- Console.Write("Please enter coefficient c: ");
- double c = double.Parse(Console.ReadLine());
- double discriminant = b * b - 4 * a * c; //calculations start here
- if (discriminant < 0)
- {
- Console.WriteLine("No real roots");
- return; //Optional
- }
- else if (discriminant == 0)
- {
- double x = - b / 2 / a;
- Console.WriteLine("Double root x1 = x2 = {0}",x);
- }
- else
- {
- double x1 = ( - b - Math.Sqrt(discriminant)) / 2 / a; // Weird, usually the first root is with + in the formula
- double x2 = ( - b + Math.Sqrt(discriminant)) / 2 / a; // I guess they want to test our attentiveness :)
- Console.WriteLine("The solution of the equation is : x1 = {0}, x2 = {1}", x1, x2);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement