Quadratic Equation
This program solves quadratic equations of the form ax2 + bx + c = 0 , It calculates the roots of the equation based on the values of a
, b
, and c
that you provide. Let's go through the code and understand how it works.
Code Breakdown
Algorithm
- Start
- Input Coefficients: Read the values of
a
,b
, andc
. - Check if
a
is 0:- If true, print "Equation is not quadratic" and exit.
- Calculate Discriminant
d
:- Compute
d
using the formula: d = b2 - 4ac
- Compute
- Check Discriminant:
- If
d > 0
, calculate two distinct real roots:root1 = (-b + sqrt(d)) / (2 * a)
root2 = (-b - sqrt(d)) / (2 * a)
- If
d == 0
, calculate one real root:root1 = -b / (2 * a)
- If
d < 0
, the roots are imaginary.
- If
- Display Results:
- Print the roots or state that they are imaginary.
- End
Code Explanation
//
denotes comments in the code, which are not executed and are not part of the code logic.
Example Flowchart
Start
|
V
Input coefficients a, b, c
|
V
Is a equal to 0?
/ \
/ \
Yes No
/ \
/ \
Print "Equation is Calculate Discriminant
not quadratic" d = b^2 - 4ac
| |
| |
V V
End Is d greater than 0?
/ \
/ \
Yes No
/ \ \
/ \ \
/ \ Is d equal to 0?
/ \ / \
/ \ Yes No
/ \ | \
+----------------------------+ +------------------------+ \
| Calculate roots: | | Calculate root: | \
| root1 = (-b + sqrt(d)) / | | root1 = -b / (2 * a) | \
| (2 * a) | +------------------------+ \
| root2 = (-b - sqrt(d)) / | | |
| (2 * a) | | |
+----------------------------+ | |
| | |
| | |
V V V
Print roots: Print "Roots are Print "Roots are
root1 and root2 real and equal" imaginary"
| | |
| | |
V V V
End End End