lmarkov

Calculate Quadratic Equation

Dec 4th, 2012
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.73 KB | None | 0 0
  1. /*
  2.  *Write a program that enters the coefficients a, b and c of a quadratic equation
  3.         a*x2 + b*x + c = 0
  4.         and calculates and prints its real roots. Note that quadratic equations may have 0, 1 or 2 real roots.
  5.  
  6.  */
  7.  
  8. using System;
  9.  
  10. class CalculateQuadraticEquation
  11. {
  12.     static void Main()
  13.     {
  14.         double coefficientA, coefficientB, coefficientC, discriminantD, DRoot2, resultX1, resultX2;
  15.  
  16.         string invalidInput = "Invalid input! Please enter value between " + double.MinValue + " and " + double.MaxValue + "!" + Environment.NewLine; ;
  17.         Console.WriteLine("ax^2 + bx + c = 0, a != 0");
  18.         Console.WriteLine("Enter value of coefficient a: a = ");
  19.         while (!(double.TryParse(Console.ReadLine(), out coefficientA) && coefficientA >= double.MinValue && coefficientA <= double.MaxValue))
  20.         {
  21.             Console.WriteLine(invalidInput);
  22.             Console.WriteLine("Enter value of coefficient a: a = ");
  23.         }
  24.  
  25.         Console.WriteLine("Enter value of coefficient b: b = ");
  26.         while (!(double.TryParse(Console.ReadLine(), out coefficientB) && coefficientB >= double.MinValue && coefficientB <= double.MaxValue))
  27.         {
  28.             Console.WriteLine(invalidInput);
  29.             Console.WriteLine("Enter value of coefficient b: b = ");
  30.         }
  31.  
  32.         Console.WriteLine("Enter value of coefficient c: c = ");
  33.         while (!(double.TryParse(Console.ReadLine(), out coefficientC) && coefficientC >= double.MinValue && coefficientC <= double.MaxValue))
  34.         {
  35.             Console.WriteLine(invalidInput);
  36.             Console.WriteLine("Enter value of coefficient c: c = ");
  37.         }
  38.  
  39.         if (coefficientA != 0)
  40.         {
  41.             discriminantD = (Math.Pow(coefficientB, 2)) - (4 * coefficientA * coefficientC);
  42.  
  43.             if (discriminantD < 0)
  44.             {
  45.                 Console.WriteLine("The quadratic equation {0}x^2 + {1}x + {2} = 0 has no real roots because discriminant D = {3}" + Environment.NewLine, coefficientA, coefficientB, coefficientC, discriminantD);
  46.             }
  47.             if (discriminantD > 0)
  48.             {
  49.                 DRoot2 = Math.Sqrt(discriminantD);
  50.                 resultX1 = (((-1) * coefficientB) + DRoot2) / (2 * coefficientA);
  51.                 resultX2 = (((-1) * coefficientB) - DRoot2) / (2 * coefficientA);
  52.  
  53.                 Console.WriteLine("The quadratic equation {0}x^2 + {1}x + {2} = 0 has two real roots: x1 = {3} and x2 = {4} and discriminant D = {5} !" + Environment.NewLine, coefficientA, coefficientB, coefficientC, resultX1, resultX2, discriminantD);
  54.             }
  55.             if (discriminantD == 0)
  56.             {
  57.                 resultX1 = (((-1) * coefficientB)) / (2 * coefficientA);
  58.                 Console.WriteLine("The quadratic equation {0}x^2 + {1}x + {2} = 0 has one real root: x1 = {3} and discriminant D = {4} !" + Environment.NewLine, coefficientA, coefficientB, coefficientC, resultX1, discriminantD);
  59.             }
  60.         }
  61.         else
  62.         {
  63.             if (coefficientB != 0)
  64.             {
  65.                 resultX1 = (-1) * coefficientC / coefficientB;
  66.                 Console.WriteLine("{0}x + {1} = 0 is not quadratic equation!" + Environment.NewLine + "After all: x = {2}" + Environment.NewLine, coefficientB, coefficientC, resultX1);                
  67.             }
  68.             else
  69.             {
  70.                 if (coefficientC != 0)
  71.                 {
  72.                     Console.WriteLine("Can't divide by 0! The equation has no solution!" + Environment.NewLine);
  73.                 }
  74.                 else
  75.                 {
  76.                     Console.WriteLine("The equation has +- infinity real roots!" + Environment.NewLine);
  77.                 }
  78.             }
  79.         }
  80.  
  81.         Main();
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment