ellapt

5.6.QuadrEquation

Dec 7th, 2012
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. using System;
  2. class QuadrEquation
  3. {
  4. static void Main()
  5. {
  6. Console.WriteLine("Read the coefficients of ax2+bx+c=0 and solve it (real roots only)");
  7. string inputVar;
  8. double a;
  9. double b;
  10. double c;
  11. double x1;
  12. double x2;
  13. do
  14. {
  15. Console.Write("Enter the coefficient a: ");
  16. }
  17. while (!double.TryParse(inputVar = Console.ReadLine(), out a) || a == 0.0);
  18. do
  19. {
  20. Console.Write("Enter the coefficient b: ");
  21. }
  22. while (!double.TryParse(inputVar = Console.ReadLine(), out b));
  23. do
  24. {
  25. Console.Write("Enter the coefficient c: ");
  26. }
  27. while (!double.TryParse(inputVar = Console.ReadLine(), out c));
  28.  
  29. string numRoots = "imaginery";
  30.  
  31. Console.Write("The quadratic equation roots are: ");
  32.  
  33. x1 = x2 = -((double)(b / (2.0 * a)));
  34. double discrim = b * b - 4.0 * a * c;
  35.  
  36. if (discrim == 0.0)
  37. {
  38. numRoots = "one";
  39. }
  40. else if (discrim > 0.0)
  41. {
  42. numRoots ="two";
  43. }
  44. switch (numRoots)
  45. {
  46. case "two":
  47. {
  48. x1 += (double)(Math.Sqrt(discrim) / (2.0 * a));
  49. x2 -= (double)(Math.Sqrt(discrim) / (2.0 * a));
  50. Console.WriteLine("x1={0} and x2={1}", x1, x2);
  51. break;
  52. }
  53. case "one":
  54. {
  55. Console.WriteLine("x1=x2={0}", x1);
  56. break;
  57. }
  58. default:
  59. {
  60. Console.WriteLine("complex.");
  61. break;
  62. }
  63. }
  64. }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment