Advertisement
G_Burlakova

QuadraticEquationSecondVersion

Feb 18th, 2014
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.85 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace QuadraticEquation
  7. {
  8.     class QuadraticEquation
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             try
  13.             {
  14.                 Console.Write("Enter the equation this way - ax^2+bx+c=0: ");
  15.                 string equation = Console.ReadLine();
  16.                 int a = int.Parse(equation.Substring(0, 1));
  17.                 int b = int.Parse(equation.Substring(4, 2));
  18.                 int c = int.Parse(equation.Substring(7, 2));
  19.                 if (a != 0)
  20.                 {
  21.                     double discriminant = (b * b) - (4 * a * c);
  22.                     if (discriminant > 0)
  23.                     {
  24.                         double firstRoot = (-b + Math.Sqrt(discriminant)) / 2 * a;
  25.                         double secondRoot = (-b - Math.Sqrt(discriminant)) / 2 * a;
  26.                         Console.WriteLine("The roots of the equation are: {0} and {1}", Math.Round(firstRoot), Math.Round(secondRoot));
  27.                     }
  28.                     else if (discriminant == 0)
  29.                     {
  30.                         double root = -b / 2 * a;
  31.                         Console.WriteLine("The only root of the equation is {0}.", root);
  32.                     }
  33.                     else
  34.                     {
  35.                         Console.WriteLine("The equation has no real roots.");
  36.                     }
  37.                 }
  38.                 else
  39.                 {
  40.                     double singleRoot = -c / b;
  41.                     Console.WriteLine("The equation has a single root - {0}.", Math.Round(singleRoot, 2));
  42.                 }
  43.             }
  44.             catch (FormatException)
  45.             {
  46.                 Console.WriteLine("You have entered some invalid data. Please try again.");
  47.             }
  48.  
  49.         }
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement