C++ Program to Find All Roots of a Quadratic Equation (Entered by User)

by | Jan 17, 2023 | C++, C++ Programs

Introduction

In this program, we will find all the roots of a quadratic equation. A quadratic equation is an equation that can be written in standard form: ax2 + bx + c. Here a, b, and c are the coefficients of a quadratic equation which can be entered by the user and can be given as a default, but in this case, we will take the input of the coefficients from the user and ‘x’ is the root which we are to find out in this program. The formula for calculating the roots of any quadratic equation is as below: (-b ± √ (b² – 4ac)) / (2a). The value of roots is only dependent on the discriminant value (D = b² – 4ac). It will tell the nature of the roots, like the roots are real, different, or complex such as:

* If the discriminant value is greater than zero (D>0), the roots are real and unique.

* If the discriminant value is equal to zero (D=0), the roots are real and equal.

* If the discriminant value is lesser than zero (D<0), the roots are complex and different.

To understand this example, you must have gone through these topics of C++ programming:

1. if, if-else

2. Nested if-else

Note: In this program, the ‘sqrt ()’ library function is used from the ‘cmath’ module to find out the square root of a number. The value of ‘a’ in ax2 + bx + c can’t be zero, and if it is zero, then the roots of the quadratic equation will be undefined.

Program

#include <iostream>
#include <cmath>
using namespace std;
int main ()
{
float a, b, c, x, y, D, real, imag;
cout << "Enter coefficients a, b and c: ";
cin >> a >> b >> c;
D = b*b - 4*a*c; //D=discriminant


if (D > 0) //for real and unique roots
{
x = (-b + sqrt(D)) / (2*a);
y = (-b - sqrt(D)) / (2*a);
cout << "Roots are real and unique" << endl;
cout << "x = " << x << endl << "y = " << y << endl;
}


else if (D == 0) //for real and same roots
{
x = (-b - sqrt(D)) / (2*a);
cout << "Roots are real and same" << endl;
cout << "x = y =" << x << endl;
}
else //for complex and imaginary roots
{
real = -b / (2*a);
imag = sqrt(-D) / (2*a);
cout << "Roots are complex and different" << endl;
cout << "x = " << real << "+" << imag << "i" << endl << "y = " << real << "-" << imag << "i" << endl;
}
return 0;
}

 

Output

Explanation

Let’s discuss the program line by line.

D = b*b – 4*a*c;

It is the major dependency of any quadratic equation because it will decide the root to be real, complex, or equal, and it is the base step for continuing with any quadratic equation.

if (D > 0)

{

x = (-b + sqrt(D)) / (2*a);

y = (-b – sqrt(D)) / (2*a);

cout << “Roots are real and unique” << endl;

cout << “x = ” << x << endl << “y = ” << y << endl;

}

If D>0, it will first square root the discriminant value and then perform the other operation with the values of the coefficients, and then it will print the values of x and y.

else if (D == 0)

{

x = (-b – sqrt(D)) / (2*a);

cout << “Roots are real and same” << endl;

cout << “x = y =” << x << endl;

}

If D==0, it will first square root the discriminant value and then perform the other operation with the values of the coefficients, and then it will print the values of x and y which is equal.

else

{

real = -b / (2*a);

imag = sqrt(-D) / (2*a);

cout << “Roots are complex and different” << endl;

cout << “x = ” << real << “+” << imag << “i” << endl << “y = ” << real << “-” << imag << “i” << endl;

}

If D<0, it will first square root the discriminant value and then perform the other operation with the values of the coefficients, and then it will print the values of x and y with the value of ‘iota’ because if anything under square root has negative value will have iota (‘i’) with it.

Note: The same program can be done using a user-defined function to calculate the square roots.

 

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.