Advertisement
Booster

QuadraticEquation

Jul 26th, 2014
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.85 KB | None | 0 0
  1. using System;
  2.  
  3. class QuadraticEquation
  4. {
  5.     static void Main()
  6.     { //ax*x + bx + c = 0
  7.         Console.WriteLine("Enter a, b, c for ax*x + bx + c = 0");
  8.         double a = double.Parse(Console.ReadLine());
  9.         double b = double.Parse(Console.ReadLine());
  10.         double c = double.Parse(Console.ReadLine());
  11.         double x1, x2;
  12.         //d = discriminant
  13.         double d = (b * b) - (4 * a * c);
  14.         if (d == 0)
  15.         {
  16.             x1 = x2 = (-b) / 2 * a;
  17.             Console.WriteLine("x1 = x2 = {0:0.000}", x1);
  18.         }
  19.         else if (d > 0)
  20.         {
  21.             x1 = (-b) + Math.Sqrt(d) / 2 * a;
  22.             x2 = (-b) - Math.Sqrt(d) / 2 * a;
  23.             Console.WriteLine("x1 = {0:0.000}, x2 = {1:0.000}", x1, x2);
  24.         }
  25.         else
  26.         {
  27.             Console.WriteLine("No real roots!");
  28.         }
  29.     }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement