Menghitung akar persamaan kuadrat
#include "stdio.h"
#include "math.h"
int main()
{
float a, b, c, D, X, X1, X2, XRiil, XImajiner;
printf("Perhitungan akar-akar persamaan aX2+bX+c=0\n");
printf("Masukkan nilai koefisien a, b, dan c:\n");
scanf("%f %f %f", &a, &b, &c);
D = b*b-4*a*c;
printf("Diskriminan = %g\n", D);
printf("Akar persamaan %gX2 + %gX + %g = 0 adalah:\n", a, b, c);
if (D > 0) {
X1 = (-b+sqrt(D))/(2*a);
X2 = (-b-sqrt(D))/(2*a);
printf("X1 = %g\n", X1);
printf("X2 = %g\n", X2);
}
else if (D == 0) {
X = -b/(2*a);
printf("X1 = X2 = %g\n", X);
}
else {
XRiil = -b/(2*a);
XImajiner = sqrt(-D)/(2*a);
printf("Akar imajiner:\n");
printf("X1 = %g+%g*i\n", XRiil, XImajiner);
printf("X2 = %g-%g*i\n", XRiil, XImajiner);
}
return 0;
}
0 komentar:
Posting Komentar