Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <math.h>
- int main()
- {
- printf("Enter the coefficients of the quadratic equation a, b, and c:\n");
- char *buf;
- char buffer[100];
- float cof_a;
- float cof_b;
- float cof_c;
- float *cof[3];
- cof[0] = &cof_a;
- cof[1] = &cof_b;
- cof[2] = &cof_c;
- fgets(buffer, 100, stdin);
- buf = strtok(buffer, " ");
- int i = 0;
- while (buf != NULL && i < 3) {
- *cof[i] = strtof(buf, NULL);
- buf = strtok(NULL, " ");
- i++;
- }
- float Discriminant = cof_b * cof_b - 4 * cof_a * cof_c;
- if (Discriminant < 0) {
- printf("No solution!");
- exit(-1);
- }
- if (Discriminant == 0) {
- float sqrt_1 = (-cof_b)/(2 * cof_a);
- printf("Root of the equation: %f", sqrt_1);
- exit(0);
- }
- if (Discriminant > 0) {
- float sqrt_1 = (-cof_b + sqrtf(Discriminant))/(2 * cof_a);
- float sqrt_2 = (-cof_b - sqrtf(Discriminant))/(2 * cof_a);
- printf("Roots of the equation: %f %f", sqrt_1, sqrt_2);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement