Thursday, March 15, 2012

Program to real roots of quadratic equation with output

// d = b^2 - 4ac

#include <iostream>
#include <cmath>          //when we used c library in c++
using namespace std;

int main()
{
    float a, b, c, d, root1, root2;


    cout << "\n Enter the value of a, b, c: " ;
    cin >> a >> b >> c;
 

    d = b*b - 4*a*c;
    cout << "\n discriminant is: " << d ;
   
    if ( d<0 )
    {
        cout << "\n Roots are imagenary.";
    }
    else
    {
        root1 = (-b + sqrt(d)) / (2*a);
        root2 = (-b - sqrt(d)) / (2*a);
        cout << "\n root1 is: " << root1 << "\n root2 is: " << root2 << endl;
    }
    return 0;
}


OUTPUT:

No comments:

Post a Comment