Solve Quadratic Equation


SUBMITTED BY: Guest

DATE: Oct. 26, 2014, 12:12 p.m.

FORMAT: Text only

SIZE: 1.7 kB

HITS: 755

  1. #include<iostream.h>
  2. #include<conio.h>
  3. #include<math.h>
  4. void main()
  5. {
  6. char choice=1;
  7. int a, b, c, d, x1, x2, loopy=0;
  8. do
  9. {
  10. clrscr();
  11. while (loopy!=80)
  12. {
  13. cout<<"-";
  14. loopy++;
  15. }
  16. cout<<"\nQuadratic Equation Solver."
  17. <<"\n";
  18. while (loopy!=0)
  19. {
  20. cout<<"-";
  21. loopy--;
  22. }
  23. cout<<"\n"
  24. <<"Enter the values of a, b and c\n";
  25. cin>>a>>b>>c;
  26. cout<<"\n";
  27. while (loopy!=80)
  28. {
  29. cout<<"-";
  30. loopy++;
  31. }
  32. cout<<"\n";
  33. d=(b*b)-(4*a*c);
  34. if (d<0)
  35. {
  36. d=(-1)*d;
  37. x1=(-b-sqrt(d)/(2*a));
  38. x2=(-b+sqrt(d)/(2*a));
  39. cout<<"The Roots are not real.\n";
  40. if (x1==0)
  41. {
  42. cout<<"x1="<<x1<<",x2="<<x2<<"i";
  43. }
  44. else if (x2==0)
  45. {
  46. cout<<"x1="<<x1<<"i,x2="<<x2;
  47. }
  48. else if (x1==x2)
  49. {
  50. cout<<"x1=x2="<<x1<<"i";
  51. }
  52. else
  53. {
  54. cout<<"x1="<<x1<<"i,x2="<<x2<<"i";
  55. }
  56. }
  57. else
  58. {
  59. x1=(-b-sqrt(d)/(2*a));
  60. x2=(-b+sqrt(d)/(2*a));
  61. cout<<"The Roots are real.\n";
  62. if (x1==x2)
  63. {
  64. cout<<"x1=x2="<<x1;
  65. }
  66. else
  67. {
  68. cout<<"x1="<<x1<<",x2="<<x2;
  69. }
  70. }
  71. cout<<"\n\n";
  72. while (loopy!=0)
  73. {
  74. cout<<"-";
  75. loopy--;
  76. }
  77. cout<<"\nPress Y to solve another equation or press any key to exit.";
  78. cin>>choice;
  79. }
  80. while (choice=='Y' || choice=='y');
  81. }

comments powered by Disqus