Guest User

Untitled

a guest
Jan 23rd, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. /* Prints roots of the quadratic equation a * x*x + b * x + c = 0 */
  5. void solveQuadraticEquation(double a, double b, double c){
  6. double root1, root2;
  7. double discriminant = findDiscr(a,b,c);
  8. if (discriminant < 0)
  9. {
  10. printf("No roots\n");
  11. }
  12. else
  13. {
  14. root1 = solvRootOne(a,b,discriminant);
  15. root2 = solvRootTwo(a,b,discriminant);
  16. }
  17.  
  18. }
  19.  
  20. int main(void) {
  21. double a,b,c;
  22. do
  23. {
  24. printf("Enter coeficients a, b, and c: ");
  25. scanf("%lf %lf %lf", &a, &b, &c);
  26. solveQuadraticEquation(a, b, c);
  27. }
  28. while (!(a==0 && b == 0 && c == 0));
  29.  
  30. return 0;
  31. }
  32.  
  33. double findDiscr(double a, double b, double c)
  34. {
  35. double discriminant = b * b - 4 * a * c;
  36. return discriminant;
  37.  
  38. }
  39.  
  40. double solvRootOne(double a,double b,double discriminant)
  41. {
  42. double root1;
  43. if (discriminant == 0){
  44. root1 = -b/(2*a);
  45. }
  46. else
  47. {
  48. root 1 = (-b + sqrt(discriminant))/(2*a);
  49. }
  50. return root1;
  51. }
  52. double solveRootTwo(double a,double b,double discriminant)
  53. {
  54. double root2;
  55. if discriminant > 0
  56. {
  57. root2 = (-b + sqrt(discriminant))/(2*a);
  58. }
  59. else
  60. {
  61. root2 = (-b - sqrt(discriminant))/(2*a);
  62. }
  63. return root2;
  64. }
Add Comment
Please, Sign In to add comment