Tutorial :simplify complex roots



Question:

i have made a program to compute roots of quauation but it does not simplify the roots.can anyone help me to simplify them

#include<stdio.h>  #include<conio.h>  #include<math.h>  void main(void)  {      int a,b,c;      float d,d2;      printf(" Enter a,b and c:");      scanf("%d %d %d",&a,&b,&c);      d=b*b-4*a*c;        if(d<0)      {          printf("(%d+i%d)/%d\n",-b,sqrt(-d),2*a) ;          printf("(%d-i%d)/%d\n",-b,sqrt(-d),2*a);      }      else      {          printf("(%d+%d)/%d\n",-b,sqrt(d),2*a);          printf("(%d-%d)/%d\n",-b,sqrt(d),2*a);      }    getch();  }  


Solution:1

You can't compute the square root of a negative number. d is negative and you're trying to find its square root. The whole point of complex solutions and the imaginary unit i is to write -1 as i^2, and then when d < 0 you have:

sqrt(d) = sqrt(i^2 * (-d)) = i*sqrt(-d)  

So change to this:

if(d<0)  {      printf("(%d+i%lf)/%d",-b,sqrt(-d),2*a);      printf("(%d-i%lf)/%d",-b,sqrt(-d),2*a);  }  

I don't know why you had parantheses around your printf arguments, I removed those.

The second %d should also be changed to %lf since sqrt returns a double.


Solution:2

If you want to compute square roots tof negative numbers, find a C99 compiler (basically, anything besides MSVC will do), include <complex.h> header, use complex data type and csqrt function.

http://en.wikipedia.org/wiki/Complex.h


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »