Kodewick
Empieza a aprender →
Básico←→0 / 50

Ecuación de segundo grado

#007Básicob * b - 4 * a * c8 min

Halla las raíces reales de ax² + bx + c = 0. El discriminante es d = b² − 4ac: si d > 0 hay dos raíces (−b ± √d) / 2a; si d = 0, una; si d < 0, ninguna real. roots(a, b, c) muestra una línea: cuántas raíces hay y luego las raíces (2 decimales). Por ejemplo, x² − 5x + 6 = 0 muestra 2 3.00 2.00.

Debería mostrar
2 3.00 2.00
2 0.28 -1.78
1 -1.00
0
main.c
#include <stdio.h>
#include <math.h>

void roots(double a, double b, double c) {
    double d = 0;
    // tu código aquí: calcula el discriminante d
    if (d > 0) {
        double x1 = 0, x2 = 0;
        // tu código aquí: usa la fórmula para calcular x1 (con +) y x2 (con −)
        printf("2 %.2f %.2f\n", x1, x2);
    } else if (d == 0) {
        printf("1 %.2f\n", -b / (2 * a));
    } else {
        printf("0\n");
    }
}

int main(void) {
    roots(1, -5, 6);
    roots(2, 3, -1);
    roots(1, 2, 1);
    roots(1, 1, 1);
    return 0;
}
Ctrl + Enter también ejecuta
Resultado

Ejecutar prueba los ejemplos. Cuando salgan bien, Enviar.