C++ program to find roots of quadratic equation

ad+1

In this program we will find roots of quadratic equation. For this purpose we will take input of three integers a, b and c. After this we will calculate discriminant. And then we will find two roots, and will store them in variable x1, and x2. And then we will find whether roots are imaginary or real.

Program:

// Program By http://solutionscpp.blogspot.com
// Author Mansoor Ahmed
// GMail ID: secguy000@gmail.com

 
#include<iostream>
#include<math.h>
using namespace std;

int main()
{
    int a=0, b=0, c=0, x1=0, x2=0, d=0;
    int real_Part=0;
    int imaginary_Part=0;
   
   
    cout<<"Enter Value of a: ";
    cin>>a;
   
    cout<<"Enter Value of b: ";
    cin>>b;
   
    cout<<"Enter Value of c: ";
    cin>>c;
   
    d=b*b-4*a*c;
   
    if (d > 0)
    {
        x1 = (-b + sqrt(d)) / (2*a);
        x2 = (-b - sqrt(d)) / (2*a);
        cout << "Roots of quadratic are real and different!" << endl;
        cout << "x1 = " << x1 << endl;
        cout << "x2 = " << x2 << endl;
       }
   
     else if (d == 0)
     {
        cout << "Roots of quadratic equation are real and same." << endl;
        x1 = (-b + sqrt(d)) / (2*a);
        cout << "x1 = x2 =" << x1 << endl;
     }
    
    
     else
     {
        real_Part = -b/(2*a);
        imaginary_Part =sqrt(-d)/(2*a);
        cout << "Roots of quadratic equation are complex and different."  << endl;
        cout << "x1 = " << real_Part << "+" << imaginary_Part << "i" << endl;
        cout << "x2 = " << real_Part << "-" << imaginary_Part << "i" << endl;
    }

}

Output:

2 comments: Leave Your Comments

  1. Hey There. I found your blog using msn. This is an extremely well written article. I’ll make sure to bookmark it and return to read more of your useful info. Thanks for the post. I will definitely return.open afp file

    ReplyDelete