Квадратное уравнение: a*x^2 + b*x + c = 0, при a!=0
// a*x^2 + b*x + c = 0, a!=0
#include <stdio.h>
#include <math.h>
int main()
{
double a=2,b=4,c=-2;
double d = b*b - 4*a*c;
int hasRoot = 0;
double x1,x2;
if (a==0) {
hasRoot = 1;
x1 = -c/b;
printf("%.2f*x^2 + %.2f*x + %.2f = 0, x=%.2f\n",a,b,c,x1);
}
else if (d==0) {
hasRoot = 1;
x1 = -b/2*a;
printf("%.2f*x^2 + %.2f*x + %.2f = 0, x=%.2f\n",a,b,c,x1);
} else if (d>0) {
hasRoot = 1;
x1 = ( -b - sqrt(d) ) / 2*a;
x2 = ( -b + sqrt(d) ) / 2*a;
printf("%.2f*x^2 + %.2f*x + %.2f = 0, x1=%.2f, x2=%.2f\n",a,b,c,x1,x2);
}
return 1;
}
Справочник алгоритмов v0.05 © 2007-2025 Igor Salnikov aka SunDoctor