# Find nature of roots and actual roots of Quadratic equation in C++

This C++ program is able to** find nature or roots and the actual roots of given quadratic equations in C++** whose values are the user has entered. There is a method called quadratic formula which is used in mathematics to get this information. This can also be implemented in programming language to do our work more efficiently and accurately.

## Nature of roots and actual roots of Quadratic equation in C++

**Programming approach**

- Define constants a, b and c and store values for quadratic equation.
- Now calculates discriminant for the given equation and show the result as per the mathematics rule.
- If the roots are real, apply the quadratic equation and show the result otherwise print that roots are imaginary.

**Quadratic formula and analysis of roots**

For a given quadratic equation ax^{2}+bx+c=0 where a, b, c are constants.

The nature of the roots of quadratic equation is determine by the determinant, D.

Where D= b^{2}-4ac

Now three cases are possible-

- If D>0 the roots are real and distinct.
- If D=0 the roots are real and equal.
- Id D<0 the roots are imaginary

Value of roots are calculated by quadratic formula:

**Program/Source code**

The following program in C++ determine the nature of the roots and calculate the actual values of them if they are real. This code is successfully compiled and tested in CodeBlocks v 16.01 in windows 10.

/* C++ program to find nature of roots and their actual values ....nature of roots is determined by value of discriminant ....value of roots is calculated by using quadratic formula */ #include <iostream> #include<math.h> using namespace std; int main() { int a,b,c; // constants of equation float x1,x2; //roots cout<<"Enter value of a: "; cin>>a; cout<<"Enter value of b: "; cin>>b; cout<<"Enter value of c: "; cin>>c; int D; D= b*b - 4*a*c; // determinant if(D>=0){ //nature of roots if(D==0) cout<<"Roots are real and equal: "; else cout<<"Roots are real and distinct: "; x1=(-b+ sqrt(D))/(2*a); // roots calculation by quadratic formula x2=(-b- sqrt(D))/(2*a); cout<<x1<<", "<<x2; } else cout<<"Roots are imaginary!"; return 0; }

**Output example**

Enter value of a: 2 Enter value of b: 3 Enter value of c: 1 Roots are real and distinct: -0.5, -1

Enter value of a: 4 Enter value of b: 1 Enter value of c: 5 Roots are imaginary!

**Program Explanation**

- Declare variables for quadratic equation constants and its roots.
- Store values of constants as provided by the user.
- Using the constants find the discriminant of the equation.
- With the help of discriminant print nature of the roots accordingly.
- In the case of determinant≥0 find the values of roots and print on the screen.

Also, read

## Leave a Reply