R
Red Dragon
I am C programming student.
My program here has an error message " illegal else without matching if " which I cannot figure out what is not matching.
Can somebody please help. Where is the mistake?
Thanks
Khoon
/* Roots of a Quadratic Equation.
12.10.05 */
#include <stdio.h>
#include <math.h>
int main (void)
{
int a; int b; int c; float x1; float x2; float E; float R; float I;
printf ("Please key in the value of constant a,b and c for finding the roots of quadratic");
printf ("equation ax%c+bx+c=0 :",253);
scanf ("%d%d%d", &a,&b,&c);
E =(b*b - 4*a*c);
if ( E > 0)
x1 = (-b+sqrt(E))/(2*a);
x2 = (-b-sqrt(E))/(2*a);
printf ("Your quadratic equation has two distinct real roots: x1=%1.6f ,x2=%1.6f",x1,x2);
else if (E = 0)
x1 = (-b+sqrt(E))/(2*a);
x2 = (-b-sqrt(E))/(2*a);
printf ("Your quadratic equation has two same roots : x1=x2=%1.6f",x1);
else
R = (-b/2*a);
I = (sqrt(E))/(2*a);
printf ("Your quadratic equation has two distinct imaginary roots :\n");
printf (" x1=%1.6f+%1.6fi , x2=%1.6f-%1.6fi\n",R,I,R,I);
return 0;
}
My program here has an error message " illegal else without matching if " which I cannot figure out what is not matching.
Can somebody please help. Where is the mistake?
Thanks
Khoon
/* Roots of a Quadratic Equation.
12.10.05 */
#include <stdio.h>
#include <math.h>
int main (void)
{
int a; int b; int c; float x1; float x2; float E; float R; float I;
printf ("Please key in the value of constant a,b and c for finding the roots of quadratic");
printf ("equation ax%c+bx+c=0 :",253);
scanf ("%d%d%d", &a,&b,&c);
E =(b*b - 4*a*c);
if ( E > 0)
x1 = (-b+sqrt(E))/(2*a);
x2 = (-b-sqrt(E))/(2*a);
printf ("Your quadratic equation has two distinct real roots: x1=%1.6f ,x2=%1.6f",x1,x2);
else if (E = 0)
x1 = (-b+sqrt(E))/(2*a);
x2 = (-b-sqrt(E))/(2*a);
printf ("Your quadratic equation has two same roots : x1=x2=%1.6f",x1);
else
R = (-b/2*a);
I = (sqrt(E))/(2*a);
printf ("Your quadratic equation has two distinct imaginary roots :\n");
printf (" x1=%1.6f+%1.6fi , x2=%1.6f-%1.6fi\n",R,I,R,I);
return 0;
}