Advertisement
kyamaliev

C#Basics HW4 Problem6 - advanced2

Mar 28th, 2014
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.41 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.             double x1 = -b / 2 / a;
  17.             double complexPartOfDiscriminant = Math.Sqrt(-discriminant)/2;
  18.             System.Console.WriteLine("The solution of the equation is : x1 = {0} - {1}i, x2 = {0} + {1}i", x1,                      complexPartOfDiscriminant);
  19.         }
  20.         else if (discriminant == 0)
  21.         {    
  22.             double x = - b / 2 / a;
  23.             Console.WriteLine("Double root x1 = x2 = {0}",x);
  24.         }
  25.         else
  26.         {
  27.             double x1 = ( - b - Math.Sqrt(discriminant)) / 2 / a; // Weird, usually the first root is with + in the formula
  28.             double x2 = ( - b + Math.Sqrt(discriminant)) / 2 / a; // I guess they want to test our attentiveness :)
  29.             Console.WriteLine("The solution of the equation is : x1 = {0}, x2 = {1}", x1, x2);
  30.         }
  31.     }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement