Monday, June 10, 2013

C++ Program to Solve the Quadratic Equation

Welcome to GSinghCodes. This is the first post on this Blog, hopefully I will continue to add more as time progresses.

Today I will explain the C Plus Plus Program which Solves the Quadratic Equation. Before we get started we need to look at the Quadratic Equations. "Standard Form" of a Quadratic Equation is ax2 + bx + c = 0, where a b and c are known, a cannot be zero and x is the unknown variable we have to find. To solve the equation we can use the quadratic formula:
Their are three types of answers you can get with the quadratic formula, Two real solutions (If the discriminant (the part inside the square root) is greater than zero) , No Real Solution (If the discriminant is strictly less than zero and One real solution(If the discriminant is equal to zero). To See Examples Click Here.

Examples To Test
 0x^2 + 0x + 0 = 0 Answer = "All Solutions"
 0x^2 + 0x + 1 = 0 Answer = "No Solutions"
 0x^2 + 2x + 1 = 0 Answer = -1/2
 1x^2 + 2x + 1 = 0 Answer = "Duplicate Solution" = -1
 1x^2 + 4x + 1 = 0 Answer = "two solutions" = d = 3.464 1st root = -0.267 | 2nd root = -3.73

C Plus Plus Program to Solve the Quadratic Equation
#include <iostream>
using namespace std;

int main ()
{
    float a, b, c, OneSolution, Solution1, Solution2, determinant;

    cout<<"Quadratic Equation Form ax^2 + bx + c \n";
    cout<<"Enter Integer for a: ";
    cin>>a;
    cout<<"Enter Integer for b: ";
    cin>>b;
    cout<<"Enter Integer for c: ";
    cin>>c;

    if (a == 0)
        if (b == 0)
            if (c == 0)
                cout<<"All Solutions" <<endl;
            else
                cout<<"NO Solutions" <<endl;
        else
        {
            OneSolution = -c/b;
            cout<<"Solution for "<<a<<"x^2 + " <<b <<"x +" <<c <<"\nx = " <<OneSolution                             <<endl;
        }
    else
    {
        determinant = b * b - 4 * a * c;
        if (determinant < 0)
            cout<<"Solution for "<<a<<"x^2 + " <<b <<"x +" <<c <<"\nNo Solution" <<endl;
        else if (determinant == 0)
        {
            cout<<"Solution for "<<a<<"x^2 + " <<b <<"x +" <<c <<"\nDuplicate Solution"                       <<endl;
            OneSolution = (-b/2) * a;
            cout<<OneSolution <<endl;
        }
        else
        {
            cout<<"two solutions" <<endl;
            Solution1 = (-b + sqrt(determinant)) / 2 * a;
            Solution2 = (-b - sqrt(determinant)) / 2 * a;
            cout<<"Solution for "<<a<<"x^2 + " <<b <<"x +" <<c <<"\nx1 = " << Solution1                           <<endl << "x2 = " <<Solution2 <<endl;
        }
    }

    system("pause");
}
 Examples Results Works
 0x^2 + 0x + 0 = 0 Answer = "All Solutions"
 0x^2 + 0x + 1 = 0 Answer = "No Solutions"
 0x^2 + 2x + 1 = 0 Answer = -1/2
 1x^2 + 2x + 1 = 0 Answer = "Duplicate Solution" = -1
 1x^2 + 4x + 1 = 0 Answer = "two solutions" = d = 3.464 1st root = -0.267 | 2nd root = -3.73

References

"Quadratic Equation." Wikipedia. Wikimedia Foundation, Last Updated 09 June. 2013. Web. 10 June 2013.http://www.en.wikipedia.org/wiki/Quadratic_equation

No comments:

Post a Comment