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

Source Code

#include <iostream>
#include <cmath> // include the cmath library for the sqrt() function

using namespace std;

int main()
{
    double a, b, c; // Declare variables to hold the coefficients of the quadratic equation
    double x1, x2; // Declare variables to hold the roots of the equation

    cout << "Enter the coefficient of x^2: "; // Prompt the user to enter the coefficient of x^2
    cin >> a; // Read the coefficient of x^2 from the standard input stream

    cout << "Enter the coefficient of x: "; // Prompt the user to enter the coefficient of x
    cin >> b; // Read the coefficient of x from the standard input stream

    cout << "Enter the constant term: "; // Prompt the user to enter the constant term
    cin >> c; // Read the constant term from the standard input stream

    // Calculate the roots of the equation using the quadratic formula
    x1 = (-b + sqrt(b*b - 4*a*c)) / (2*a);
    x2 = (-b - sqrt(b*b - 4*a*c)) / (2*a);

    cout << "The roots of the equation are: " << x1 << " and " << x2 << endl; // Print the roots

    return 0;
}

Code Explanation

This program declares three variables a, b, and c of type double to hold the coefficients of the quadratic equation, and two variables x1 and x2 of type double to hold the roots of the equation. It then uses the cout object to print prompts asking the user to enter the coefficients and the cin object to read the coefficients from the standard input stream.

To find the roots of the equation, the program uses the quadratic formula:

x = (-b +- sqrt(b^2 - 4ac)) / (2a)

where a, b, and c are the coefficients of the equation, and x is the root. The + and - signs represent the two possible roots of the equation.

The program calculates the roots using the formula and stores them in the variables x1

Explain (cmath)

<cmath> is a header file in the C++ standard library that defines a set of mathematical functions and macros. It is part of the C++ Standard Template Library (STL) and is defined in the namespace std.

The functions defined in <cmath> are a subset of the C math library (math.h) and provide a range of mathematical operations such as trigonometric functions, exponential and logarithmic functions, power and absolute value functions, and more.

Here are some examples of functions defined in <cmath>:

  • sqrt(): calculates the square root of a number
  • pow(): raises a number to a specified power
  • exp(): calculates the exponential of a number
  • log(): calculates the natural logarithm of a number
  • abs(): calculates the absolute value of a number
  • sin(): calculates the sine of an angle
  • cos(): calculates the cosine of an angle
  • tan(): calculates the tangent of an angle

To use the functions defined in <cmath>, you need to include the header file in your C++ program using the #include preprocessor directive:

#include <cmath>

You can then access the functions using the std:: namespace prefix, or by using the using namespace std; directive.

Note: C++ Complete Examples