- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

A quadratic equation is in the form ax^{2} + bx + c. The roots of the quadratic equation are given by the following formula −

There are three cases −

b^{2} < 4*a*c - The roots are not real i.e. they are complex

b^{2} = 4*a*c - The roots are real and both roots are the same.

b^{2} > 4*a*c - The roots are real and both roots are different

The program to find the roots of a quadratic equation is given as follows.

#include<iostream> #include<cmath> using namespace std; int main() { int a = 1, b = 2, c = 1; float discriminant, realPart, imaginaryPart, x1, x2; if (a == 0) { cout << "This is not a quadratic equation"; }else { 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 << "Root 1 = " << x1 << endl; cout << "Root 2 = " << x2 << endl; } else if (discriminant == 0) { cout << "Roots are real and same." << endl; x1 = (-b + sqrt(discriminant)) / (2*a); cout << "Root 1 = Root 2 =" << x1 << endl; }else { realPart = (float) -b/(2*a); imaginaryPart =sqrt(-discriminant)/(2*a); cout << "Roots are complex and different." << endl; cout << "Root 1 = " << realPart << " + " << imaginaryPart << "i" <<end; cout << "Root 2 = " << realPart << " - " << imaginaryPart << "i" <<end; } } return 0; }

Roots are real and same. Root 1 = Root 2 =-1

In the above program, first the discriminant is calculated. If it is greater than 0, then both the roots are real and different.

This is demonstrated by the following code snippet.

if (discriminant > 0) { x1 = (-b + sqrt(discriminant)) / (2*a); x2 = (-b - sqrt(discriminant)) / (2*a); cout << "Roots are real and different." << endl; cout << "Root 1 = " << x1 << endl; cout << "Root 2 = " << x2 << endl; }

If the discriminant is equal to 0, then both the roots are real and same. This is demonstrated by the following code snippet.

else if (discriminant == 0) { cout << "Roots are real and same." << endl; x1 = (-b + sqrt(discriminant)) / (2*a); cout << "Root 1 = Root 2 =" << x1 << endl; }

If the discriminant is less than 0, then both the roots are complex and different. This is demonstrated by the following code snippet.

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

- Related Questions & Answers
- C program to find the Roots of Quadratic equation
- Java program to find the roots of a quadratic equation
- How to write a C program to find the roots of a quadratic equation?
- Finding roots of a quadratic equation – JavaScript
- Program to find number of solutions in Quadratic Equation in C++
- How to Solve Quadratic Equation using Python?
- Absolute difference between sum and product of roots of a quartic equation?
- C program to find the solution of linear equation
- Program to find equation of a plane passing through 3 points in C++
- Program to find out the value of a given equation in Python
- Program to find max value of an equation in Python
- Find the number of primitive roots modulo prime in C++.
- C++ Program to Implement Hash Tables with Quadratic Probing
- Finding all solutions of a Diophantine equation using JavaScript
- Find number of solutions of a linear equation of n variables in C++

Advertisements