Solved

# Complex Numbers in C

Posted on 2007-11-26

Could someone give me sone help on working out complex root using C?

Here is my code so far:

#include <stdio.h>

#include <math.h>

double t1=0,t2=0,t3=0;

double quad1(double t1,double t2, double t3);

double quad2(double t1,double t2, double t3);

main()

{

printf("Enter the X square coeff: ");

scanf("%lf",&t1);

printf("Enter the X coeff: ");

scanf("%lf",&t2);

printf("Enter the last term: ");

scanf("%lf",&t3);

printf("The 2 solutions of your equation are: %lf and %lf",quad1(t1,t2,t3),quad2(t1,t2,t3));

}

double quad1(double t1,double t2, double t3)

{

double ans1=0,det1=0;

det1= pow(t2,2) - (4*t1*t3); // b^2 - 4ac

ans1= (((-1)*t2) + pow(det1,0.5))/(2*t1);

return(ans1);

}

double quad2(double t1,double t2, double t3)

{

double ans2=0,det2=0;

det2= pow(t2,2) - (4*t1*t3); // b^2 - 4ac

ans2= (((-1)*t2) - pow(det2,0.5))/(2*t1);

return(ans2);

}

As you can see, it only works out real roots

Cheers