CONTROL STRUCTURE INTRODCUTION_EXAMPLE QUADRATIC EQUATION

So far our programs have a straight line of execution. You have entered some values or assign some values to variables then the program would use this variables to execute a formula and the program would show you the result. Let’s take a quadratic equation. The code is given below.

#include "stdafx.h"
#include "iostream"
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
       double root1, root2, a, b, c, root;
       cout << "Enter coefficients a, b, c: ";
       cin >> a >> b >> c;
       root = sqrt(b*b - 4 * a*c);
       root1 = 0.5*(root - b) / a;
       root2 = -0.5*(root + b) / a;
       cout << "The solutions are:" << root1 << "and" << root2 << ".\n";
       getchar();
       return 0;
      
}


In this program to solve quadratic equation, we could enter numbers a, b and c which make b*b – 4 * a * c NEGATIVE. If negative the program would fail. So what to do now? So far the program had a simple structure now we will introduce Iteration and branching techniques. But before we upgrade the quadratic equation we need to get familiar with new operators. 
Share on Google Plus