Home »
C++ Programs
C++ program to find all roots of a quadratic equation using class
Submitted by Shubh Pachori, on August 08, 2022
Problem statement
Given coefficients of an equation, we have to find all roots of a quadratic equation using class using the class and object approach.
Example:
Input:
Enter Coefficient of a: 2
Enter Coefficient of b: 5
Enter Coefficient of c: 3
Output:
Roots are real and different.
Root 1 = -1
Root 2 = -1.5
C++ code to find all roots of a quadratic equation using class and object approach
#include <iostream>
#include <cmath>
using namespace std;
// create a class
class Quadratic {
// private data members
private:
float a, b, c;
// public functions
public:
// getCoefficient() function to insert
// the coefficients of the equation
void getCoefficient() {
cout << "Enter Coefficient of a:";
cin >> a;
cout << "Enter Coefficient of b:";
cin >> b;
cout << "Enter Coefficient of c:";
cin >> c;
}
// roots() function to find out the nature and
// values of both root of the equation
void roots() {
// float type variable for operations
float root_1, root_2, discriminant, real_part, imaginary_part;
// calculating discriminant
discriminant = b * b - 4 * a * c;
// if discriminant is greater than 0 the
// both the roots are real different
if (discriminant > 0) {
// calculating first root
root_1 = (-b + sqrt(discriminant)) / (2 * a);
// calculating second root
root_2 = (-b - sqrt(discriminant)) / (2 * a);
// printing them
cout << "Roots are real and different." << endl;
cout << "Root 1 = " << root_1 << endl;
cout << "Root 2 = " << root_2 << endl;
}
// if discriminant is equal to 0 then
// both the root are real and same
else if (discriminant == 0) {
cout << "Roots are real and same." << endl;
// calculating roots
root_1 = -b / (2 * a);
cout << "Root 1 = Root 2 =" << root_1 << endl;
}
// else roots are complex and different
else {
// calculating real part of roots
real_part = -b / (2 * a);
// calculating imaginary part of roots
imaginary_part = sqrt(-discriminant) / (2 * a);
// printing them
cout << "Roots are complex and different." << endl;
cout << "Root 1 = " << real_part << "+" << imaginary_part << "i" << endl;
cout << "Root 2 = " << real_part << "-" << imaginary_part << "i" << endl;
}
}
};
int main() {
// create a class
Quadratic Q;
// calling getCoefficient() function to insert coefficients value
Q.getCoefficient();
// calling roots() function to find roots
Q.roots();
return 0;
}
Output
Enter Coefficient of a: 4
Enter Coefficient of b: 1
Enter Coefficient of c: 4
Roots are complex and different.
Root 1 = -0.125+0.992157i
Root 2 = -0.125-0.992157i
Explanation
In the above code, we have created a class Quadratic, three float type data members a, b and c to store the coefficients of the quadratic equation, and public member functions getCoefficient() and roots() to store and find out roots of the given quadratic equation.
In the main() function, we are creating an object Q of class Quadratic, reading coefficients of the quadratic equation by the user using the getCoefficient() function, and finally calling the roots() member function to find out the roots of the given coefficients of the quadratic equation. The roots() function contains the logic to find out the roots of the given coefficients of the quadratic equation and printing the result.
C++ Class and Object Programs (Set 2) »