Solving quadratic equations or finding the roots of equations of second degree is a popular problem in many programming languages. The equations of second degree which resemble the standard form: ax 2 +bx+c=0, are known as quadratic equations. A large number of quadratic equations need to be solved in mathematics, physics and engineering. For a quadratic equation ax2+bx+c = 0 (where a, b and c are coefficients), it's roots is given by following the formula. The term b 2 -4ac is known as the discriminant of a quadratic equation. The discriminant tells the nature of the roots. If discriminant is greater than 0, the roots are real and different.
The C++ program to solve quadratic equations in standard form is a simple program based on the quadratic formula. Given the coefficients as input, it will solve the equation and output the roots of the equation.
This is a simple program is intended for intermediate level C++ programmers.
For a quadratic equation ax2+bx+c = 0 (where a, b and c are coefficients), it's roots is given by following the formula. The term b 2 -4ac is known as the discriminant of a quadratic equation. The discriminant tells the nature of the roots. If discriminant is greater than 0, the roots are real and different. Write a C program to find all roots of a quadratic equation using if else. How to find all roots of a quadratic equation using if else in C programming. Logic to find roots of quadratic equation in C programming.
The program is compiled using Dev-C++ 4.9.9.2 version installed on a Windows 7 64-bit PC. You may try other standard C compilers and the program will still work if you use the correct C libraries.
Problem Definition
In this program, we solve the quadratic equation using the formula
When
Where a, b and c are coefficient of the equation which is in the standard form.
How to compute?
The steps to compute the quadratic equation is given below.
Step1:
The program request the input coefficient values a, b and c. When the user input the values, it will compute two terms t1 and t3 and an intermediate term t2.
Step2:
The function term2 ()
is called in step 2 and returned value of function is assigned to t2
. The term2 ()
function receives the coefficient values – a, b, c and compute the value for t2.
The term ()
function returns and assign value of b2 – 4ac
to t2
and it is useful in understanding the root of the quadratic equation.
For example,
If (t2 < 0), then the roots are not real
If (t2 0) then, there is exactly one root value.
C++ Compiler
If (t2 > 0) then there are two root values.
The above condition is checked and printed with output values. Now we need to compute the roots and display the output values.
Step3:
A term t3
is assigned value after taking square root of t2.
Step4:
Finally, we have t1 and t3 to compute two roots of a quadratic equation.
Then root1
and root
are calculated and printed immediately.
Flowchart – Program for Quadratic Equations
Dev C++ 5.11
To understand flow of logic of this program, see the flowchart below.
Program Code – Program for Quadratic Equation
Output
Dev C++ For Windows 10
Dev-c Gta 5
The output of the above program is given below.