Loading...
C++ Program to Find All Roots of a Quadratic Equation.

C++ Program to Find All Roots of a Quadratic Equation.

In this example, we will learn program that accepts coefficients of a quadratic equation from the user and displays the roots (both real and complex roots depending upon the discriminant).

To understand this example, you should have the knowledge of the following C++ programming topics:


Program to Find All Roots of a Quadratic Equation

For a quadratic equation ax2+bx+c = 0 (where a, b and c are coefficients), it's roots is given by following the formula.

Example

Input  :  a = 1, b = -2, c = 1
Output :  Roots are real and same
  1

Input  :  a = 1, b = 7, c = 12
Output :  Roots are real and different
  -3, -4

Input  :  a = 1, b = 1, c = 1
Output :  Roots are complex 
  -0.5 + i1.73205
  -0.5 - i1.73205

Formula to find root of an quadratic equation

The term b2-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.
  • If discriminant is equal to 0, the roots are real and equal.
  • If discriminant is less than 0, the roots are complex and different.

Example:

If b2 < 4*a*c, then roots are complex
(not real).
For example roots of x2 + x + 1, roots are
-0.5 + i1.73205 and -0.5 - i1.73205

If b2 == 4*a*c, then roots are real 
and both roots are same.
For example, roots of x2 - 2x + 1 are 1 and 1

If b2 > 4*a*c, then roots are real 
and different.
For example, roots of x2 - 7x - 12 are 3 and 4

Example 1: Program to Find Roots of a Quadratic Equation.

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

int main() {

    float a, b, c, x1, x2, discriminant, realPart, imaginaryPart;
    cout << "Enter coefficients a, b and c: ";
    cin >> a >> b >> c;
    discriminant = b*b - 4*a*c;
    
    if (discriminant > 0) {
        x1 = (-b + sqrt(discriminant)) / (2*a);
        x2 = (-b - sqrt(discriminant)) / (2*a);
        cout << "Roots are real and different." << endl;
        cout << "x1 = " << x1 << endl;
        cout << "x2 = " << x2 << endl;
    }
    
    else if (discriminant == 0) {
        cout << "Roots are real and same." << endl;
        x1 = -b/(2*a);
        cout << "x1 = x2 =" << x1 << endl;
    }

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

    return 0;
}

Output

Enter coefficients a, b and c: 4
5
1
Roots are real and different.
x1 = -0.25
x2 = -1

In this program, sqrt() library function is used to find the square root of a number.


Example 2: Program to Find Roots of a Quadratic Equation.

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

int main() {

    float a, b, c, d, root1, root2;
    cout << "Enter coefficients a, b and c: ";
    cin >> a >> b >> c;
    d = b*b - 4*a*c;
    
    if (d == 0) {
        root1 = (-b + sqrt(d)) / (2*a);
        root2=root1;
        cout << "Roots are real and equal." << endl;
    }
    
    else if (d > 0) {
        root1 = (-b + sqrt(d)) / (2*a);
        root2 = (-b - sqrt(d)) / (2*a);
        cout << "Roots are real and different." << endl;
    }

    else {
        root1 = -b/(2*a);
        root2 =sqrt(-d)/(2*a);
        cout << "Roots are complex and different."  << endl;
    }
    cout << "\nroot1 = " << root1 << "\nroot2 =" << root2;
    return 0;
}

Output

Enter coefficients a, b and c: 1.0
10.0
1.5
Roots are real and different.

root1 = -0.0502524
root2 =-9.94975

In these program, cmathpreprocessor and sqrt() library function is used to find the square root of a number.


Next Example

We hope that this Example helped you develop better understanding of the concept of "Find the Roots of a Quadratic Equation" in C++.

Keep Learning : )

In the next Example, we will learn about C++ Find Sum of Natural Numbers.


- Related Topics