Problem: Write a program in C++ to find the complex roots of the quadratic equation 4x2-2x+3=0.

Code: 

//This program is written for the case the roots are complex
#include <iostream>
#include <cmath>
using namespace std;

int main(){
    float a, b, c, d, real, img;

    cout << "Enter a: ";
    cin >> a;

    cout << "Enter b: ";
    cin >> b;
    cout << "Enter c: ";
    cin >> c;

    d = (pow(b,2)-(4*a*c));

    real= -b/(2*a);
    img = sqrt(-d)/(2*a);

    cout << "The roots are " << real <<" + "<<img << "i and " << real <<" - "<<img<< "i" ;
}

Output: 

Enter a: 4
Enter b: -2
Enter c: 3
The roots are 0.25 + 0.829156i and 0.25 - 0.829156i
Process returned 0 (0x0)   execution time : 6.306 s
Press any key to continue.

أحدث أقدم