Advertisement
AnitaN

04.ConsoleInputOutputHomework/06.QuadraticEquation

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