Advertisement
ohad

Untitled

Sep 8th, 2016
2,781
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //------------------------------------------------------------------------------------------------------------------------------
  2. //                                                                  Exercise 5
  3. //                                                                  ----------
  4. //
  5. // General : The program will get the three coefficients and it will solve the equation if its possible.
  6. //
  7. // Input   : Three coefficients.
  8. //
  9. // Process : The program solves the equation according to the well known formula. In case that the equation cant be solved, the program will alert the user.
  10. //
  11. // Output  : Prints the equation and its sulotion.
  12.  
  13. //
  14. //------------------------------------------------------------------------------------------------------------------------------
  15. // Programmer: Ohad Ozcohen
  16. // Date: 9.9.2016
  17. //------------------------------------------------------------------------------------------------------------------------------
  18.  
  19. #include <stdio.h>
  20. #include <math.h>
  21. void main(void)
  22. {
  23.     float a,b,c;
  24.     float delta;
  25.     float root1, root2;
  26.     printf("Please enter a: ");
  27.     scanf_s("%f", &a);
  28.     printf("Please enter b: ");
  29.     scanf_s("%f", &b);
  30.     printf("Please enter c: ");
  31.     scanf_s("%f", &c);
  32.     delta = b*b - 4 * a*c;
  33.     if (delta > 0)
  34.     {
  35.         float test = sqrt(delta);
  36.         // sqrt() function returns square root
  37.         root1 = (-b + sqrt(delta)) / (2 * a);
  38.         root2 = (-b - sqrt(delta)) / (2 * a);
  39.  
  40.         printf("root1 = %.2lf and root2 = %.2lf", root1, root2);
  41.     }
  42.     if (delta == 0)
  43.     {
  44.         root1 = root2 = -b / (2 * a);
  45.  
  46.         printf("root1 = root2 = %.2f;", root1);
  47.     }
  48.     if (delta<0)
  49.     {
  50.         printf("There is no solution to the equation");
  51.     }
  52.    
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement