coasterka

#4QuadraticEquationRoots

Mar 19th, 2014
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.57 KB | None | 0 0
  1. using System;
  2. class QuadraticEquationRoots
  3.     {
  4.         static void Main(string[] args)
  5.         {
  6.             Console.WriteLine("Enter a quadratic equation of this type: ax^2 + bx + c = 0.");
  7.             string equation = Console.ReadLine();
  8.             int cA = int.Parse(equation.Substring(0, equation.IndexOf("x^2")).Replace(" ", ""));
  9.             equation = equation.Substring(equation.IndexOf("x^2") + 3);
  10.             int cB = int.Parse(equation.Substring(0, equation.IndexOf("x")).Replace(" ", ""));
  11.             equation = equation.Substring(equation.IndexOf("x") + 1);
  12.             int cC = int.Parse(equation.Substring(0, equation.IndexOf("=")).Replace(" ", ""));
  13.             Console.WriteLine("a = " + cA);
  14.             Console.WriteLine("b = " + cB);
  15.             Console.WriteLine("c = " + cC);
  16.             double D = (cB * cB) - (4 * cA * cB);
  17.             Console.WriteLine("Discriminant: " + D);
  18.             double b1 = 0;
  19.             double b2 = 0;
  20.             if (D == 0)
  21.             {
  22.                 double result1 = b1 = b2 = ((-cB) / (2 * cA));
  23.                 Console.WriteLine("b1 = b2 = " + result1);
  24.             }
  25.             else if (D > 0)
  26.             {
  27.                 double result2 = b1 = ((-cB) + Math.Sqrt(D)) / (2 * cA);
  28.                 double result3 = b2 = ((-cB) - Math.Sqrt(D)) / (2 * cA);
  29.                 Console.WriteLine("b1 = " + result2);
  30.                 Console.WriteLine("b2 = " + result3);
  31.             }
  32.             else
  33.             {
  34.                 Console.WriteLine("Equation has no solution");
  35.             }
  36.         }
  37.     }
Advertisement
Add Comment
Please, Sign In to add comment