tags:

views:

57

answers:

2

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();
}
+4  A: 

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.

IVlad
I like to add, that `sqrt` returns a double and not an integer, so the `%d` should also be changed.
Lucas
yes sorry by mistake i wrote them ,thanks for ur help
fahad
@lucas Thanks bro :)
fahad
+1  A: 

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

el.pescado