/* * Examples of if statement * * Compute two roots of the quadratic equation a x^2 + b x + c = 0 * */ #include #include int main(void) { double a, b, c; double disc, root_1, root_2, root_1_real, root_1_imag, root_2_real, root_2_imag; printf("Input the coefficients of the quadratic equation a, b, c > "); scanf("%lf%lf%lf",&a,&b,&c); printf("coefficients a = %.2f, b = %.2f, c = %.2f\n",a,b,c); /* compute two roots for discriminant > 0 */ disc = pow(b,2) - 4 * a * c; if (disc >= 0) { root_1 = (-b + sqrt(disc)) / (2 * a); root_2 = (-b - sqrt(disc)) / (2 * a); printf("root_1 = %.2f, root_2 = %.2f",root_1,root_2); } else { root_1_real = -b / (2 * a); root_1_imag = +sqrt(-disc) / (2 * a); root_2_real = -b / (2 * a); root_2_imag = -sqrt(-disc) / (2 * a); /* printf("root_1_real = %.2f, root_1_imag = %.2f\n", root_1_real, root_1_imag); printf("root_2_real = %.2f, root_2_imag = %.2f", root_2_real, root_2_imag); */ printf("root_1 = (real,imag) = (%.2f,%.2f)\n", root_1_real, root_1_imag); printf("root_2 = (real,imag) = (%.2f,%.2f)\n", root_2_real, root_2_imag); } return (0); }