C++ program to find solutions of quadratic equation

In this tutorial, we will learn to find the roots or solutions of a quadratic equation in C++. In mathematics, these equations are used in fields such as simplification of expressions, equations of a circle and other conic sections, etc. Here, we will learn a method to find the roots of these equations, and a C++ program that calculates the roots of a given quadratic equation.

Quadratic equation

The general quadratic equation is as follows –

Ax^2 + Bx + C = 0

where,

  • A, B, and C are known values.
    A is the coefficient of the term containing x^2. Also, A cannot be 0.
    B is the coefficient of the term containing x.
    C is a constant value.
  • x is an unknown value or variable

The name ‘quadratic’ means square because the equations contain the square of the unknown variable. The quadratic equations are of degree 2.

For example –
5x^2 + 4x + 1 = 0
x^2 + 2x + 1 = 0

Finding roots of a quadratic equation

Every quadratic equation has exactly two roots. The roots can be equal or distinct, and real or complex. So, to find the nature of roots, calculate the discriminant using the following formula –

Discriminant, D = B^2 – 4AC

  • Case 1 – D < 0
    If D is less than 0, then the roots and distinct and complex.
  • Case 2 – D = 0
    If D is equal to 0, then the roots are equal and real.
  • Case 3 – D > 0
    If D is greater than 0, then both the roots are real and distinct.

To find both the roots, we use the formula given below –

Root1 = ( -B + square_root(D) ) / 2A
Root2 = ( -B – square_root(D) ) / 2A

Program to find roots of a quadratic equation in C++

Now, we will see a program that calculates the roots of a quadratic equation using C++. The program takes the coefficients i.e. A, B, and C from the user and then finds the roots. The C++ program to find roots of the equation is –

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
  float a,b,c,x1,x2,d,real,imaginary;
  cout<<"Quadratic Equation : ax^2 + bx + c";
  cout<<"\nEnter coefficient of a : ";
  cin>>a;
  cout<<"Enter coefficient of b : ";
  cin>>b;
  cout<<"Enter coefficient of c : ";
  cin>>c;

  d=b*b-4*a*c;
  if(d>0)
  {
    cout<<"Roots are real and distinct";
    x1=(-b + sqrt(d)) / (2*a);
    x2=(-b - sqrt(d)) / (2*a);
    cout<<"\nx1 = "<<x1;
    cout<<"\nx2 = "<<x2;
  }
  else if(d==0)
  {
    cout<<"Roots are real and same";
    x1=(-b + sqrt(d)) / (2*a);
    cout<<"\nx1 = "<<x1;
    cout<<"\nx2 = "<<x1;
  }
  else
  {
    cout<<"Roots are complex and distinct";
    real = -b/(2*a);
    imaginary = sqrt(-d) / (2*a);
    cout<<"\nx1 = "<<real<<"+"<<imaginary<<"i";
    cout<<"\nx2 = "<<real<<"-"<<imaginary<<"i";
  }
  return 0;
}

In this program, we find the square root using the in-built sqrt() function of the ‘cmath’ library. The program displays both the roots of the given quadratic equation.

C++ program output

The output of the above program is –

siddharth@siddharth-Lenovo-Y520-15IKBN:~/cpp$ g++ quadratic.cpp
siddharth@siddharth-Lenovo-Y520-15IKBN:~/cpp$ ./a.out
Quadratic Equation : ax^2 + bx + c
Enter coefficient of a : 1
Enter coefficient of b : 2
Enter coefficient of c : -8
Roots are real and distinct
x1 = 2
x2 = -4
siddharth@siddharth-Lenovo-Y520-15IKBN:~/cpp$

The user has entered the value of A, B, and C as 1, 2, and -8 respectively. The roots of the equations always satisfy the equations.
For example –

x^2 + 2x - 8 = 0

Root1 = 2
        (2)^2 + 2*(2) - 8
      = 4 + 4 - 8
      = 8 - 8
      = 0
Root2 = -4
        (-4)^2 + 2*(-4) - 8
      = 16 - 8 -8
      = 16 - 16
      = 0

So, both the roots satisfy the equation.

Thank you for reading this tutorial. I hope it helps you a lot.

Leave a Reply

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