fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define VOID 0
  4.  
  5. double sqrt(double n) {
  6.  
  7. double x = n, y = 1.0, e = 0.000001;
  8.  
  9. while(x - y > e) {
  10.  
  11. x = (x + y) / 2;
  12.  
  13. y = n / x;
  14.  
  15. }
  16. return x;
  17. }
  18.  
  19. void NatureRootsQuadraticEquation(double a, double b, double c) {
  20.  
  21. double S = -b/a,
  22. P = c/a,
  23. discriminant = b*b - 4*a*c,
  24. x1, x2;
  25. x1 = (-b + sqrt(discriminant)) / (2 * a);
  26. x2 = (-b - sqrt(discriminant)) / (2 * a);
  27. printf("S=%.2lf P=%.2lf\n",S,P);
  28. printf("Delta = %.2lf\n\tx1=%lf\n\tx2=%lf\n", discriminant, x1, x2);
  29.  
  30. if(discriminant >= 0) {
  31.  
  32. if(S >= 0) {
  33.  
  34. if(P > 0) {
  35.  
  36. printf("%s","x1>0; x2>0");
  37.  
  38. } else if(P < 0) {
  39.  
  40. printf("%s","x1>0; x2<0; |x1| < |x2|");
  41.  
  42. } else {
  43.  
  44. printf("%s","x1=0; x2 >0");
  45. }
  46. //it means S < 0
  47. } else {
  48.  
  49. if(P > 0) {
  50.  
  51. printf("%s","x1<0; x2<0");
  52.  
  53. } else if(P < 0) {
  54.  
  55. printf("%s","x1>0; x2<0");
  56.  
  57. } else {
  58.  
  59. printf("%s","x1=0; x2 <0");
  60. }
  61. }
  62. } else {
  63. printf("Imaginary");
  64. }
  65. printf("\n");
  66. }
  67.  
  68. int main(int argc, char const *argv[]) {
  69. //declare three variable of double
  70. double a, b, c;
  71. a = 1;
  72. b = 3;
  73. c = -2;
  74. //if the number of the arguments is greater than one
  75. if(argc > 1){
  76. //convert string to double
  77. a = atof(argv[1]);
  78. b = atof(argv[2]);
  79. c = atof(argv[3]);
  80. }
  81.  
  82. //tests
  83. if(b < 0 && c < 0) {
  84. printf("%.3lfx^2 - %.3lfx - %.3lf = 0\n", a, -b, -c);
  85. } else if(b < 0) {
  86. printf("%.3lfx^2 - %.3lfx + %.3lf = 0\n", a, -b, c);
  87. } else if(c < 0) {
  88. printf("%.3lfx^2 + %.3lfx - %.3lf = 0\n", a, b, -c);
  89. } else if(a > 0 && b > 0 && c > 0){
  90. printf("%.3lfx^2 + %.3lfx + %.3lf = 0\n", a, b, c);
  91. }
  92. //solve
  93. printf("%s\n", "Output:");
  94. NatureRootsQuadraticEquation(a, b, c);
  95.  
  96. return VOID;
  97. }
Success #stdin #stdout 0s 5304KB
stdin
Standard input is empty
stdout
1.000x^2 + 3.000x - 2.000 = 0
Output:
S=-3.00 P=-2.00
Delta = 17.00
	x1=0.561553
	x2=-3.561553
x1>0; x2<0