lmarkov

Quadratic Equation

Dec 1st, 2012
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.85 KB | None | 0 0
  1. /*
  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.  
  5. using System;
  6.  
  7. class QuadraticEquation
  8. {
  9.     static void Main()
  10.     {
  11.         double coefficientA, coefficientB, coefficientC, discriminantD;
  12.  
  13.         string invalidInput = "Invalid input! Please enter value between " + double.MinValue + " and " + double.MaxValue + "!" + Environment.NewLine; ;
  14.         Console.WriteLine("ax^2 + bx + c = 0, a != 0");
  15.         Console.WriteLine("Enter value of coefficient a: a = ");
  16.         while (!(double.TryParse(Console.ReadLine(), out coefficientA) && coefficientA >= double.MinValue && coefficientA <= double.MaxValue && coefficientA != 0))
  17.         {
  18.             Console.WriteLine(invalidInput + "Coefficient \"a\" must be different from 0!" + Environment.NewLine);
  19.             Console.WriteLine("Enter value of coefficient a: a = ");
  20.         }
  21.  
  22.         Console.WriteLine("Enter value of coefficient b: b = ");
  23.         while (!(double.TryParse(Console.ReadLine(), out coefficientB) && coefficientB >= double.MinValue && coefficientB <= double.MaxValue))
  24.         {
  25.             Console.WriteLine(invalidInput);
  26.             Console.WriteLine("Enter value of coefficient b: b = ");
  27.         }
  28.  
  29.         Console.WriteLine("Enter value of coefficient c: c = ");
  30.         while (!(double.TryParse(Console.ReadLine(), out coefficientC) && coefficientC >= double.MinValue && coefficientC <= double.MaxValue))
  31.         {
  32.             Console.WriteLine(invalidInput);
  33.             Console.WriteLine("Enter value of coefficient c: c = ");
  34.         }
  35.  
  36.         discriminantD = (Math.Pow(coefficientB, 2)) - (4 * coefficientA * coefficientC);
  37.  
  38.         if (discriminantD < 0)
  39.         {
  40.             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);
  41.         }
  42.         if (discriminantD > 0)
  43.         {
  44.             double DRoot2 = Math.Sqrt(discriminantD);
  45.             double resultX1 = (((-1) * coefficientB) + DRoot2) / (2 * coefficientA);
  46.             double resultX2 = (((-1) * coefficientB) - DRoot2) / (2 * coefficientA);
  47.  
  48.             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);
  49.         }
  50.         if (discriminantD == 0)
  51.         {
  52.             double resultX1 = (((-1) * coefficientB)) / (2 * coefficientA);
  53.             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);
  54.         }
  55.  
  56.         Main();
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment