C++ Program to find roots of a quadratic equation

Here we are discussing a C++ program to find all roots of a quadratic equation.

What is a quadratic equation?

In math, we define a quadratic equation as an equation of degree 2, meaning that the highest exponent of this function is 2. The standard form of a quadratic is y = ax^2 + bx + c, where a, b, and c are numbers and a cannot be 0. 
Example : 6x^2 + 11x – 35 = 0.

How to find the roots of a quadratic equation?

For a quadratic equation ax2 + bx + c = 0, The roots are calculated using the formula, x = (-b ± √ (b² - 4ac) )/2a 
Where a, b, and c are coefficients.
b2 - 4ac is known as the discriminant of a quadratic equation.
The discriminant tells the nature of the roots.
•    If the discriminant is greater than 0, the roots are real and different.
•    If the discriminant is equal to 0, the roots are real and equal.
•    If the discriminant is less than 0, the roots are complex and different.

C++ program to find the roots of a quadratic equation

Read 3 coefficients from the user to the float type variables a, b, c. First, calculate the value of discriminant  by using the formula
Discriminant = b² - 4ac. Store the value to a float type variable discrim.
For the discriminant value, we have three conditions.
This can be done by using the if….else if….else statement.
Syntax:
If ( condition1 )
{ code block 1 }
else if ( condition2 )
{ code block 2 }
else
{ code block3 ) }

If the value of discrim is greater than zero ( discrim > 0 ) calculate the roots by the formula  (-b ± √ (b² - 4ac) )/2a. For calculating the square root the sqrt function can be used.

The sqrt() function in C++ returns the square root of a number. This function is defined in the cmath header file
R1=  (-b + √ (b² - 4ac) )/2a and 
R2 =  (-b - √ (b² - 4ac) )/2a
Print R1 and R2.
Else if discriminant value is equal to zero ( discrim = = 0 )
Root R1 = root R 2 
R1 = -b / 2a
Print R1
Else there are two parts real part and imaginary parts.
Real part is calculated by using the formula rpart = -b / 2a;
Imaginary part is calculated by using the formula ipart  =  i√- (b² - 4ac) )/2a
Print the imaginary part and the real part.
Finish the program.

Algorithm

Step 1:  Call the header file iostream.

Step 2: Call the header file cmath.

Step 3:Use the namespace std

Step 4:Open the integer type main function; int main().


Step 5: Declare float variables; a, b, c, R1, R2, discrim, ipart,  rpart
 

Step 6: Print a message to enter coefficients a, b, c;
 

Step 7: Read the numbers into the variables a, b, and c.
 

Step 8: calculate the discriminant discrim =  b2 - 4ac.
 

Step 9: if the discrim >0
             Then R1=  (-b + sqrt(discrim )/2a and 
              R2 =  (-b - sqrt(discrim )/2a
              Print R1 and R2
              Else if discrim = 0
              Then R1 = R2
               R1 = -b / 2a
               Else if discrim < 0
               Real part rpart = -b / 2a;
               Imaginary part ipart = i√- sqrt(discrim)/2a
               Print rpart and ipart

Step 10: Exit

 

 

C++ Source Code

                                          #include <iostream>
#include <cmath>
using namespace std;

int main() {

    float a, b, c, R1, R2, discrim, rPart, iPart;
    cout << "Enter coefficients a, b and c: ";
    cin >> a >> b >> c;
    discrim= b*b - 4*a*c;
    
    if (discrim> 0) {
        R1 = (-b + sqrt(discrim)) / (2*a);
        R2 = (-b - sqrt(discrim)) / (2*a);
        cout << "Roots are real and different." << endl;
        cout << "R1 = " << R1 << endl;
        cout << "R2 = " << R2 << endl;
    }
    
    else if (discrim == 0) {
        cout << "Roots are real and same." << endl;
        R1 = -b/(2*a);
        cout << "R1 = R2 =" << R1 << endl;
    }

    else {
        rPart = -b/(2*a);
        iPart =sqrt(-discrim)/(2*a);
        cout << "Roots are complex and different."  << endl;
        cout << "x1 = " << rPart << "+" << iPart << "i" << endl;
        cout << "x2 = " << rPart << "-" << iPart << "i" << endl;
    }

    return 0;
}
                                      

OUTPUT

Enter coefficients a, b and c: 5
3
6
Roots are complex and different.
x1 = -0.3+1.05357i
x2 = -0.3-1.05357i