Advertisement
kyamaliev

C#Basics HW4 Problem6

Mar 27th, 2014
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.35 KB | None | 0 0
  1. using System;
  2. class QuadraticEquation
  3. {
  4.     static void Main()
  5.         {
  6.             Console.WriteLine("This program solves quadratic equations of the form ax^2 + bx + c = 0");
  7.             Console.Write("Please enter coefficient a: ");
  8.             double a = double.Parse(Console.ReadLine());
  9.             Console.Write("Please enter coefficient b: ");
  10.             double b = double.Parse(Console.ReadLine());
  11.             Console.Write("Please enter coefficient c: ");
  12.             double c = double.Parse(Console.ReadLine());
  13.             double discriminant = b * b - 4 * a * c;     //calculations start here
  14.             if (discriminant < 0)
  15.             {
  16.         Console.WriteLine("No real roots");
  17.                 return;                                 //Optional
  18.             }
  19.             else if (discriminant == 0)
  20.             {    
  21.                 double x = - b / 2 / a;
  22.                 Console.WriteLine("Double root x1 = x2 = {0}",x);
  23.             }
  24.             else
  25.             {
  26.                 double x1 = ( - b - Math.Sqrt(discriminant)) / 2 / a; // Weird, usually the first root is with + in the formula
  27.                 double x2 = ( - b + Math.Sqrt(discriminant)) / 2 / a; // I guess they want to test our attentiveness :)
  28.                 Console.WriteLine("The solution of the equation is : x1 = {0}, x2 = {1}", x1, x2);
  29.             }
  30.         }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement