fork download
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <ctype.h>
  4.  
  5.  
  6.  
  7. double quad_formula(double a, double b, double c){
  8.  
  9. double add_solution,sub_solution;
  10.  
  11. add_solution = (-b + sqrt((b * b) - (4*(a * c)) ))/(2 *a);
  12. sub_solution = (-b - sqrt((b * b) - (4*(a * c)) ))/(2 *a);
  13.  
  14. if (!isnan(add_solution) && !isnan(sub_solution)){
  15. if(add_solution = sub_solution){
  16. printf("There is one real solution: %.2lf", add_solution);
  17. }else{
  18. printf("There are 2 real solutions\n");
  19. printf("Solution 1: %.2lf", add_solution);
  20. printf("Solution 2: %.2lf", sub_solution);
  21. }
  22.  
  23. }else if(!isnan(add_solution)){
  24. printf("There is one real solution: %.2lf", add_solution);
  25.  
  26. }else if(!isnan(sub_solution)){
  27. printf("There is one real solution: %.2lf", sub_solution);
  28.  
  29. }else{
  30. printf("There are no real solutions");
  31. }
  32.  
  33. return 0.0;
  34. }
  35.  
  36. int main() {
  37. double a,b,c;
  38. printf("Given a quadratic equation of the form a*x^2 + b * x + c\n");
  39. printf("Please enter a:");
  40. scanf("%lf", &a);
  41. printf("Please enter b:");
  42. scanf("%lf", &b);
  43. printf("Please enter c:");
  44. scanf("%lf", &c);
  45. quad_formula(a,b,c);
  46.  
  47.  
  48. return 0;
  49. }
Success #stdin #stdout 0.01s 5284KB
stdin
45
stdout
Given a quadratic equation of the form a*x^2 + b * x + c
Please enter a:Please enter b:Please enter c:There are no real solutions