Advertisement
Guest User

QuadraticEquation

a guest
Mar 28th, 2014
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.24 KB | None | 0 0
  1. class QuadraticEquation
  2.     {
  3.         static void Main(string[] args)
  4.         {
  5.             double a, b, c;
  6.             Console.WriteLine("ax^2 + bx + c = 0");
  7.             Console.WriteLine("");
  8.             Console.Write("Enter a: ");
  9.             bool aIsValid = double.TryParse(Console.ReadLine(), out a);
  10.             Console.Write("Enter b: ");
  11.             bool bIsValid = double.TryParse(Console.ReadLine(), out b);
  12.             Console.Write("Enter c: ");
  13.             bool cIsValid = double.TryParse(Console.ReadLine(), out c);
  14.  
  15.             if (aIsValid && bIsValid && cIsValid)
  16.             {
  17.                 //D = b^2 - 4*a*c
  18.                 double d = b * b - (4 * a * c);
  19.                 double x1 = (-b + Math.Sqrt(d)) / (2 * a);
  20.                 double x2 = (-b - Math.Sqrt(d)) / (2 * a);
  21.                 if (double.IsNaN(x1) && double.IsNaN(x2))
  22.                 {
  23.                     Console.WriteLine("No real roots.");
  24.                 }
  25.                 else
  26.                 {
  27.                     Console.WriteLine("{0} {1}", x1, x2);
  28.                 }
  29.             }
  30.             else
  31.             {
  32.                 Console.WriteLine("Please enter valid data.");
  33.                 Console.Beep();
  34.             }
  35.         }
  36.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement