archangelmihail

SolveQuadraticEquation

Nov 17th, 2013
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. using System;
  2.  
  3. class SolveQuadraticEquation
  4. {
  5. static void Main(string[] args)
  6. {
  7. /* Write a program that reads the coefficients a, b and c of a quadratic equation
  8. ax2+bx+c=0 and solves it (prints its real roots). */
  9.  
  10. Console.WriteLine("Enter values for a, b and c of your quadratic equation:");
  11. Console.WriteLine("a=");
  12. double a = double.Parse(Console.ReadLine());
  13. Console.WriteLine("b=");
  14. double b = double.Parse(Console.ReadLine());
  15. Console.WriteLine("c=");
  16. double c = double.Parse(Console.ReadLine());
  17. double sqrtpart = b * b - 4 * a * c;
  18. double x, x1, x2, img;
  19. if (sqrtpart > 0)
  20. {
  21. x1 = (-b + Math.Sqrt(sqrtpart)) / (2 * a);
  22. x2 = (-b - Math.Sqrt(sqrtpart)) / (2 * a);
  23. Console.WriteLine("Two Real Solutions: {0,8:f4} or {1,8:f4}", x1, x2);
  24. }
  25. else if (sqrtpart < 0)
  26. {
  27. sqrtpart = -sqrtpart;
  28. x = -b / (2 * a);
  29. img = Math.Sqrt(sqrtpart) / (2 * a);
  30. Console.WriteLine("The Equation Has No Real Solutions!");
  31. Console.WriteLine("Two Imaginary Solutions: {0,8:f4} + {1,8:f4} i or {2,8:f4} + {3,8:f4} i", x, img, x, img);
  32. }
  33. else
  34. {
  35. x = (-b + Math.Sqrt(sqrtpart)) / (2 * a);
  36. Console.WriteLine("One Real Solution: {0,8:f4}", x);
  37. }
  38. }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment