| functions/eq-sec-degree.c - En funktion der finder rødder i en andengradsligning. | Lektion 5 - slide 5 : 30 Program 2 |
#include <stdio.h>
#include <math.h>
/* Find and print the roots in a * x*x + b * x + c = 0 */
void solveQuadraticEquation(void){
double a, b, c, discriminant;
printf("Enter coeficients a, b, and c (a != 0): ");
scanf("%lf %lf %lf", &a, &b, &c);
discriminant = b * b - 4 * a * c;
if (discriminant < 0)
printf("No roots\n");
else if (discriminant == 0)
printf("One root: %f\n", -b/(2*a));
else
printf("Two roots: %f and %f\n",
(-b + sqrt(discriminant))/(2*a),
(-b - sqrt(discriminant))/(2*a) );
}
int main(void) {
int i = 3;
/* ... */
/* Solve three equations: */
for (i = 1; i <= 3; i++)
solveQuadraticEquation();
/* ... */
return 0;
}