Finding Roots of Quadratic Equation using C++

Roots of Quadratic Equation Google

This is a simple programming tutorial for finding roots (x1 and x2) of the quadratic equation (ax2 + bx + c = 0) using C++. In this tutorial, I have used Dev C++ v5.11 software for compiling the C++ program.

About Quadratic Equation

In algebra, a quadratic equation (quadratussquare2) is an equation having the form ax2 + bx + c = 0, where x represents an unknown number (aka variable), and a, b, and c represent known numbers (aka constants).

The formula for solving roots of the quadratic equation is \[x=\frac{-b\pm \sqrt{{{b}^{2}}-4ac}}{2a}\].
The term b2 - 4ac is known as the determinant (D) of the quadratic equation, which determines the type of the root. Roots of the quadratic equation are broadly categorized into three categories as shown below:

Discriminant Formula Roots
b2 - 4ac = D > 0 \[{{x}_{1}}=\frac{-b+\sqrt{{{b}^{2}}-4ac}}{2a}\] Real and Unequal
\[{{x}_{2}}=\frac{-b-\sqrt{{{b}^{2}}-4ac}}{2a}\]
b2 - 4ac = D = 0 \[{{x}_{1}}={{x}_{2}}=\frac{-b}{2a}\] Real and Equal
b2 - 4ac = D < 0 \[{{x}_{1}}=\frac{-b}{2a}+i\left( \frac{\sqrt{-({{b}^{2}}-4ac)}}{2a} \right)\] Real and Imaginary
\[{{x}_{2}}=\frac{-b}{2a}-i\left( \frac{\sqrt{-({{b}^{2}}-4ac)}}{2a} \right)\]

In some cases, the roots are real and rational/irrational when determinant D > 0. For example,

Case 1: Roots are Real and Irrational (or Not Perfect Square)
\[{{x}_{1}}|{{x}_{2}}=\sqrt{8},\sqrt{\frac{1}{3}},\sqrt{1.3},-\sqrt{11},\pi ,e,0.3030030003....,etc\]

Case 2: Roots are Real and Rational (or Perfect Square)
\[{{x}_{1}}|{{x}_{2}}=\frac{1}{3},\sqrt{\frac{16}{9}},0.97,1\frac{2}{3},0.\overline{21},etc\]

The graph of a quadratic equation is known as a parabola, which can instantly be generated through Google search by term “x^2-2x+1”. If a > 0, then its vertex points down. If a < 0, then its vertex points up. If a = 0, the graph is not a parabola and a straight line.

Source Code

// Finding Roots of Quadratic Equation
#include <iostream>
#include <math.h>
#include <conio.h>
using namespace std;
main()
{
  system("cls");// system("clear"); for Linux
  int a, b, c;
  float disc, r1, r2;
  cout<<"+--------------------------------------------------+\n";
  cout<<"| Roots of the Quadratic Equation Ax\xFD + Bx + C = 0 |\n";
  cout<<"+--------------------------------------------------+\n";
  cout<<"\nEnter non zero co-efficients a, b, and c: ";
  cin>>a>>b>>c;
  if((a == 0) && (b == 0) && (c == 0)) {
    cout<<"\nNo Possible Roots Found.\n";
  } else {
    disc = ((b * b) - (4 * a * c));
    if(disc > 0) {
      cout<<"\nRoots are Real and Unequal:\n\n";
      r1 = (-b - (sqrt(disc))) / (2 * a);
      r2 = (-b + (sqrt(disc))) / (2 * a);
      cout<<"Root 1: "<<r1;
      cout<<"\nRoot 2: "<<r2<<"\n";
    } else if(disc < 0) {
      r1 = -b / (2 * a);
      if (r1 == 0) {
      	cout<<"\nRoots are Imaginary:\n\n";
        cout<<"Root 1: "<<sqrt(-disc)/(2 * a)<<"i\n";
        cout<<"Root 2: -"<<sqrt(-disc)/(2 * a)<<"i\n";
      } else {
        cout<<"\nRoots are Real and Imaginary:\n\n";
		cout<<"Root 1: "<<r1<<" + "<<sqrt(-disc)/(2 * a)<<"i\n";
        cout<<"Root 2: "<<r1<<" - "<<sqrt(-disc)/(2 * a)<<"i\n";
      }
    } else {
      cout<<"\nRoots are Real and Equal:\n\n";
      r1 = -b / (2 * a);
      cout<<"Root 1: "<<r1;
      cout<<"\nRoot 2: "<<r1<<"\n";
    }
  }
  getch();
}

Output

Roots of Quadratic Equation Output

Comments

Most Popular Posts

TNEB Bill Calculator

TNEB Bill Calculator (New)

Technical Questions