Skip to content
ExamplesEspañolWorkspace →

Quadratic Equation

Solves ax² + bx + c = 0, with real or complex roots.

Level
Intermediate
Topic:
Math
Concepts:
Input & output
Si
Math
Open in Codoritmo

Run, change and debug this example in your browser.

What you'll learn

The discriminant picks one of three cases with nested Si blocks. Raiz takes the square root, and an early check avoids dividing when a = 0.

Code

quadratic.psc · 26 lines
Algoritmo EcuacionCuadratica    Definir a, b, c, discriminante, x1, x2 Como Real    Escribir "Coeficientes de ax² + bx + c = 0"    Escribir "a:"    Leer a    Escribir "b:"    Leer b    Escribir "c:"    Leer c    Si a = 0 Entonces        Escribir "Con a = 0 no es una ecuación cuadrática."    SiNo        discriminante <- b ^ 2 - 4 * a * c        Si discriminante > 0 Entonces            x1 <- (-b + Raiz(discriminante)) / (2 * a)            x2 <- (-b - Raiz(discriminante)) / (2 * a)            Escribir "Dos raíces reales: x1 = ", x1, ", x2 = ", x2        SiNo            Si discriminante = 0 Entonces                Escribir "Una raíz doble: x = ", -b / (2 * a)            SiNo                Escribir "Raíces complejas: ", -b / (2 * a), " ± ", Raiz(-discriminante) / (2 * a), "i"            FinSi        FinSi    FinSiFinAlgoritmo

Flowchart

Each block in the flowchart matches one instruction in the code.

Algoritmo EcuacionCuadraticaSíNoSíNoSíNoAlgoritmo EcuacionCuadraticaAlgoritmo EcuacionCuadraticaDefinir a, b, c, discriminante, x1, x2 Como RealDefinir a, b, c,discriminante, x1, x2 ComoRealEscribir "Coeficientes de ax² + bx + c = 0"Escribir "Coeficientes de ax²+ bx + c = 0"Escribir "a:"Escribir "a:"Leer aLeer aEscribir "b:"Escribir "b:"Leer bLeer bEscribir "c:"Escribir "c:"Leer cLeer cSi a = 0 Entoncesa = 0Escribir "Con a = 0 no es una ecuación cuadrática."Escribir "Con a = 0 no es unaecuación cuadrática."discriminante <- b ^ 2 - 4 * a * cdiscriminante <- b ^ 2 - 4 * a* cSi discriminante > 0 Entoncesdiscriminante > 0x1 <- (-b + Raiz(discriminante)) / (2 * a)x1 <- (-b +Raiz(discriminante)) / (2 * a)x2 <- (-b - Raiz(discriminante)) / (2 * a)x2 <- (-b -Raiz(discriminante)) / (2 * a)Escribir "Dos raíces reales: x1 = ", x1, ", x2 = ", x2Escribir "Dos raíces reales:x1 = ", x1, ", x2 = ", x2Si discriminante = 0 Entoncesdiscriminante = 0Escribir "Una raíz doble: x = ", -b / (2 * a)Escribir "Una raíz doble: x =", -b / (2 * a)Escribir "Raíces complejas: ", -b / (2 * a), " ± ", Raiz(-discriminante) / (2 * a), "i"Escribir "Raíces complejas: ",-b / (2 * a), " ± ",Raiz(-discriminante) / (2 *…FinAlgoritmoFinAlgoritmo

Sample run

This is what the program shows when it runs. The answers typed by the user are highlighted.

Coeficientes de ax² + bx + c = 0a:User answer: 1b:User answer: -3c:User answer: 2Dos raíces reales: x1 = 2, x2 = 1

Challenge

When a = 0, solve the linear equation bx + c = 0 instead of just warning.

Show hint

The solution is x = -c / b, but first make sure b is not 0.

Open in Codoritmo
  • Body Mass Index

    Computes your BMI and classifies it as underweight, normal and so on.

    Intermediate
  • Parking Fee

    Charges every started hour, with a daily maximum.

    Intermediate
  • Prime Numbers

    Finds every prime number up to a limit.

    Intermediate