Advertisement
kyamaliev

C#Basics HW4 Problem6 - advanced

Mar 28th, 2014
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.13 KB | None | 0 0
  1. using System;
  2. using System.Numerics;
  3. class GeneralizedQuadraticEquation
  4. {
  5.     static void Main()
  6.     {
  7.         Console.WriteLine("This program solves  general (non-real roots included) quadratic equations of the form ax^2 + bx + c = 0");
  8.         Console.Write("Please enter coefficient a: ");
  9.         double a = double.Parse(Console.ReadLine());
  10.         Console.Write("Please enter coefficient b: ");
  11.         double b = double.Parse(Console.ReadLine());
  12.         Console.Write("Please enter coefficient c: ");
  13.         double c = double.Parse(Console.ReadLine());
  14.         double discriminant = b * b - 4 * a * c;     //calculations start here
  15.         Complex squarerootDiscriminant = Complex.Sqrt(discriminant);
  16.         if (discriminant == 0)
  17.         {
  18.             double x = -b / 2 / a;
  19.             Console.WriteLine("Double root x1 = x2 = {0}", x);
  20.         }
  21.         else
  22.         {
  23.             Complex x1 = (-b - squarerootDiscriminant) / 2 / a;
  24.             Complex x2 = (-b + squarerootDiscriminant) / 2 / a;
  25.             Console.WriteLine("The solution of the equation is : x1 = {0}, \r\n x2 = {1}", x1, x2);
  26.         }
  27.     }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement