-6

I also entered include<math.h> but it still doesnt work. People are saying to enter -Im but im new to this where do I put -Im and how do I fix this.

 #include <stdio.h>
#include <stdlib.h>
#include<math.h>

int main()
{
    float a=0, b=0, c=0, root1=0, root2=0;

    printf("Enter the value of a,b and c to determine the roots\n");
    scanf("%f%f%f",&a,&b,&c);

    root1=(-b+sqrt(b*b-4*a*c))/(2*a);
    root1=(-b-sqrt(b*b-4*a*c))/(2*a);

    printf("The first roots of the quadratic equation are\nFirst root=%.1f\nSecond root=%.1f",root1,root2);


    return 0;
}
Natan Streppel
  • 5,759
  • 6
  • 35
  • 43

2 Answers2

0

Two things: first you copy pasted "root1" twice so you will lose the "plus" value and root2 will be zero. Second, for the benefits of others, the problem is most probably at compile time and the googled answer is there:

http://www.cs.cf.ac.uk/Dave/C/node17.html

And you should test for imaginary values:

    if(b*b-4*a*c < 0){
      printf("error: complex solution unsupported, see http://en.wikipedia.org/wiki/Square_root\n");
      exit(1);
    }
Sebastien
  • 1,439
  • 14
  • 27
0

You have a copy-paste bug here:

root1=(-b+sqrt(b*b-4*a*c))/(2*a);
root1=(-b-sqrt(b*b-4*a*c))/(2*a);

should be:

root1=(-b+sqrt(b*b-4*a*c))/(2*a);
root2=(-b-sqrt(b*b-4*a*c))/(2*a);

Also you may need to link with the math library, e.g.

$ gcc -Wall foo.c -o foo -lm
Paul R
  • 208,748
  • 37
  • 389
  • 560