fork download
  1. #include <math.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. float userinput(char prompt[]); //Function Prototype
  6. void root(float a, float b, float c);
  7.  
  8. int main()
  9. {
  10.  
  11. float a,b,c;
  12.  
  13. a=userinput("Enter the value for a:"); //Function Call
  14. b=userinput("Enter the value for b:");
  15. c=userinput("Enter the value for c:");
  16.  
  17. printf("The Equation you entered is \n%fx^2+%fx+%f=0\n", a, b, c);
  18. root(a, b, c);
  19. return 0;
  20. }
  21.  
  22. void root(float a, float b, float c)
  23. {
  24. float D,x,x1,x2,x3,x4;
  25. D = b*b - 4*a*c;
  26. if(D>0)
  27. {
  28. printf("There are two real roots, the roots are: ");
  29. x1 = ((-b+(sqrt(D)))/(2*a));
  30. x2 = ((-b-(sqrt(D)))/(2*a));
  31. printf("%.2f and %.2f" ,x1 , x2);
  32. }
  33.  
  34. if(D==0)
  35. {
  36. printf("There is one real root, the root is: ");
  37. x = ((-b)/(2*a));
  38. printf("%.2f",x);
  39. }
  40.  
  41. if(D<0)
  42. {
  43. printf("There are two imaginary roots. The roots are: ");
  44. x3 = ((-b/2*a)+(sqrt(fabs(D))/(2*a)));
  45. printf("%.2fi and ", x3);
  46. x4 = ((-b/2*a)-(sqrt(fabs(D))/(2*a)));
  47. printf("%.2fi", x4);
  48. }
  49. }
  50.  
  51. float userinput(char prompt[]) //Function definition
  52. {
  53. float answer;
  54. int status;
  55. do
  56. {
  57. printf("%s",prompt);
  58.  
  59. status=scanf("%f", &answer);
  60. if(status!=1)
  61. {
  62. fflush(stdin);
  63. printf("INPUT ERROR!\n");
  64. }
  65. }
  66. while(status!=1);
  67.  
  68. return answer;
  69. }
  70.  
  71.  
  72.  
Success #stdin #stdout 0s 2116KB
stdin
1
2
3
stdout
Enter the value for a:Enter the value for b:Enter the value for c:The Equation you entered is 
1.000000x^2+2.000000x+3.000000=0
There are two imaginary roots. The roots are: 0.41i and -2.41i