cplusplus
cplusplus

 

 

Introduction

A quadratic equation is a second-order polynomial equation in a single variable, commonly written as:

ax² + bx + c = 0

The values of ‘a’, ‘b’, and ‘c’ are constants, and ‘x’ represents the unknown variable. The solutions to the quadratic equation are obtained using the quadratic formula:

x = (-b ± √(b² – 4ac)) / 2a

This program will help you solve quadratic equations by calculating the roots of the equation based on the values of ‘a’, ‘b’, and ‘c’.

Objective

The objective of this program is to compute the roots of a quadratic equation. Depending on the discriminant (b² – 4ac), the program will return either two real roots, one real root, or no real roots. This will be handled using conditional statements and the quadratic formula.

Code

#include 
#include 
using namespace std;

int main() {
    double a, b, c, discriminant, root1, root2;

    // Taking input from the user for the coefficients a, b, and c
    cout << "Enter the coefficients of the quadratic equation (a, b, c): "; cin >> a >> b >> c;

    // Checking if the equation is valid
    if (a == 0) {
        cout << "The value of 'a' cannot be zero for a quadratic equation." << endl; return 1; } // Calculating the discriminant discriminant = b * b - 4 * a * c; // Checking the nature of the roots if (discriminant > 0) {
        root1 = (-b + sqrt(discriminant)) / (2 * a);
        root2 = (-b - sqrt(discriminant)) / (2 * a);
        cout << "The equation has two real roots: " << root1 << " and " << root2 << endl;
    }
    else if (discriminant == 0) {
        root1 = -b / (2 * a);
        cout << "The equation has one real root: " << root1 << endl;
    }
    else {
        cout << "The equation has no real roots." << endl;
    }

    return 0;
}

Program Structure and Explanation

The program follows a simple structure:

  • Input: The user is prompted to enter the coefficients ‘a’, ‘b’, and ‘c’ for the quadratic equation.
  • Validation: The program checks if the value of ‘a’ is zero. If ‘a’ is zero, the equation is not quadratic, and the program exits with a message.
  • Discriminant Calculation: The discriminant (b² – 4ac) is calculated. The discriminant helps determine the nature of the roots.
  • Root Calculation: Based on the value of the discriminant:
    • If the discriminant is positive, two real roots are calculated.
    • If the discriminant is zero, one real root is calculated.
    • If the discriminant is negative, the program informs the user that there are no real roots.

How to Run the Program

To run this program, follow these steps:

  1. Open a C++ Integrated Development Environment (IDE) or a text editor (like Visual Studio, Code::Blocks, or any text editor) and create a new C++ file.
  2. Copy and paste the provided code into your C++ file.
  3. Save the file with a “.cpp” extension (e.g., quadratic_solver.cpp).
  4. Compile the program using your C++ compiler. For example, using the command: g++ quadratic_solver.cpp -o quadratic_solver
  5. Run the compiled program using the command: ./quadratic_solver
  6. Input the coefficients when prompted, and the program will display the roots of the quadratic equation.
© 2024 Learn Programming

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

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

error

Enjoy this blog? Please spread the word :)