#include <stdio.h>
#include <math.h>
int main() {
double a,b,c, disc, p, q, x1, x2;
printf("請輸入 a,b,c: ");
scanf("%lf%lf%lf", &a, &b, &c);
disc = b*b - 4*a*c;
if (disc >= 0) {
p = -b / (2*a);
q = sqrt(disc) / (2*a);
x1 = p + q; x2 = p - q;
printf("x1 = %lf\nx2 = %lf\n", x1, x2);
} else {
printf("無實數解\n");
}
return 0;
}
輸入: 2 3 1
輸出:
x1 = -0.500000
x2 = -1.000000
Δ = b²-4ac 決定根的性質。sqrt 需 math.h,編譯鏈接 -lm。%lf 輸入輸出。