A more complicated example is to solving a quadratic equation with one unknown.
$$ax^2+bx+c=0$$
where \(a \neq 0\). The formula of roots of above quadric equation is:
$$r_{1,2}=\frac{-b\pm\sqrt{b^2-4ac}}{2a}$$
/*
Description: This program is to solving ax^2+bx+c=0
Author: Liutong Xu
*/
#include <stdio.h>
#include <math.h>
int main(void)
{
float a, b, c;
float delta, root1, root2;
scanf("%f%f%f", &a, &b, &c);
delta = b * b - 4 * a * c;
if (delta > 0)
{
// In case of two different roots
root1 = (-b + sqrt(delta))/(2 * a);
root2 = (-b - sqrt(delta))/(2 * a);
printf("The equation has two different roots: %f and %f.\n", root1, root2);
}
else if (delta == 0) // Bad implementation!
{
// In case of unique root
root1 = -b / (2 * a);
printf("The equation has unique root %f.\n", root1);
}
else
{
// No real root
printf("The equation has no real root\n");
}
return 0;
}
stdin