Advertisement
nadicako

Quadratic Equation

Oct 16th, 2014
519
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.81 KB | None | 0 0
  1. using System;
  2.  
  3. class QuadraticEquation
  4. {
  5.     static void Main()
  6.     {
  7.         double a = 0;
  8.         double b = 0;
  9.         double c = 0;
  10.         double x1 = 0;
  11.         double x2 = 0;
  12.         double x = 0;
  13.  
  14.         Console.WriteLine("Enter value for a:");
  15.         a = double.Parse(Console.ReadLine());
  16.  
  17.         Console.WriteLine("Enter value for b:");
  18.         b = double.Parse(Console.ReadLine());
  19.  
  20.         Console.WriteLine("Enter value for c:");
  21.         c = double.Parse(Console.ReadLine());
  22.  
  23.         double rootPart = (b * b) - (4 * a * c);
  24.  
  25.         if (rootPart > 0)
  26.         {
  27.             x1 = (-b) - Math.Sqrt(rootPart) / 2 * a;
  28.             x2 = (-b) + Math.Sqrt(rootPart) / 2 * a;
  29.             Console.WriteLine(x1);
  30.             Console.WriteLine(x2);
  31.         }
  32.         else if (rootPart < 0)
  33.         {
  34.             Console.WriteLine("No real roots");
  35.         }
  36.         else
  37.         {
  38.             x = (-b) - Math.Sqrt(rootPart) / 2 * a;
  39.             Console.WriteLine(x);
  40.         }
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement