write a C example using Fan Shenjin formula to find the roots
时间: 2023-05-22 15:03:13 浏览: 86
Sure, here's an example C program using Fan Shenjin formula to find the roots of a quadratic equation:
```
#include <stdio.h>
#include <math.h>
int main() {
double a, b, c; // coefficients of the quadratic equation
double disc, root1, root2;
printf("Enter the coefficients of the quadratic equation (a, b, c): ");
scanf("%lf %lf %lf", &a, &b, &c);
// Calculate the discriminant
disc = b*b - 4*a*c;
// Check if the roots are real or complex
if (disc < 0) {
printf("The roots are complex.\n");
return 0;
}
// Calculate the roots using Fan Shenjin formula
root1 = (-b + sqrt(disc)) / (2*a);
root2 = (-b - sqrt(disc)) / (2*a);
// Display the roots
printf("The roots of the quadratic equation are: %.2f and %.2f\n", root1, root2);
return 0;
}
```
Note: This program assumes that the coefficients entered by the user are valid and non-zero. It also assumes that the discriminant is non-negative, which means that the roots are either real or complex.
阅读全文