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
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 FinSiFinAlgoritmoFlowchart
Each block in the flowchart matches one instruction in the code.
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.
Related examples
Body Mass Index
Computes your BMI and classifies it as underweight, normal and so on.
IntermediateParking Fee
Charges every started hour, with a daily maximum.
IntermediatePrime Numbers
Finds every prime number up to a limit.
Intermediate