Advertisement
puma7953

Untitled

Nov 17th, 2020
534
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.11 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <math.h>
  5.  
  6. int main()
  7. {
  8.     printf("Enter the coefficients of the quadratic equation a, b, and c:\n");
  9.  
  10.     char *buf;
  11.     char buffer[100];
  12.     float cof_a;
  13.     float cof_b;
  14.     float cof_c;
  15.     float *cof[3];
  16.     cof[0] = &cof_a;
  17.     cof[1] = &cof_b;
  18.     cof[2] = &cof_c;
  19.  
  20.     fgets(buffer, 100, stdin);
  21.     buf = strtok(buffer, " ");
  22.     int i = 0;
  23.  
  24.     while (buf != NULL && i < 3) {
  25.         *cof[i] = strtof(buf, NULL);
  26.         buf = strtok(NULL, " ");
  27.         i++;
  28.     }
  29.  
  30.     float Discriminant = cof_b * cof_b - 4 * cof_a * cof_c;
  31.  
  32.     if (Discriminant < 0) {
  33.         printf("No solution!");
  34.         exit(-1);
  35.     }
  36.  
  37.     if (Discriminant == 0) {
  38.         float sqrt_1 = (-cof_b)/(2 * cof_a);
  39.         printf("Root of the equation: %f", sqrt_1);
  40.         exit(0);
  41.     }
  42.  
  43.     if (Discriminant > 0) {
  44.         float sqrt_1 = (-cof_b + sqrtf(Discriminant))/(2 * cof_a);
  45.         float sqrt_2 = (-cof_b - sqrtf(Discriminant))/(2 * cof_a);
  46.         printf("Roots of the equation: %f %f", sqrt_1, sqrt_2);
  47.     }
  48.  
  49.     return 0;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement