Advertisement
Teodor92

Solver

Oct 30th, 2012
665
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.10 KB | None | 0 0
  1. /* Write a program that reads the coefficients
  2.  * a, b and c of a quadratic equation ax2+bx+c=0
  3.  * and solves it (prints its real roots).
  4.  */
  5.  
  6. using System;
  7.  
  8. class QuadraticEquationSolver
  9. {
  10.     static void Main()
  11.     {
  12.         double a = double.Parse(Console.ReadLine());
  13.         double b = double.Parse(Console.ReadLine());
  14.         double c = double.Parse(Console.ReadLine());
  15.         double discriminant = b * b - 4 * a * c;
  16.         if (a == 0)
  17.         {
  18.             Console.WriteLine("There is one real root - x1: {0}", -c/b);
  19.         }
  20.         else if (discriminant > 0)
  21.         {
  22.             double xOne = (-b + Math.Sqrt(discriminant)) / 2 * a;
  23.             double xTwo = (-b - Math.Sqrt(discriminant)) / 2 * a;
  24.             Console.WriteLine("There are two real roots - x1: {0} and x2: {1}", xOne, xTwo);
  25.         }
  26.         else if (discriminant == 0)
  27.         {
  28.             double xOne = -(b / 2 * a);
  29.             Console.WriteLine("There is one real root - x1: {0}", xOne);
  30.         }
  31.         else
  32.         {
  33.             Console.WriteLine("There are no real roots");
  34.         }
  35.     }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement