asked Jan 20, 2019
double a, b, c, d, x1, x2;
printf("ax^2+bx+c=0\n");
printf("a=\n");
scanf("%lf", &a);
printf("b=\n");
scanf("%lf", &b);
printf("c=\n");
scanf("%lf", &c);
d = b*b-4*a*c;
// condition for real and different roots
if (d > 0);
{
// sqrt() function returns square root
x1 = (-b+sqrt(d))/(2*a);
x2 = (-b-sqrt(d))/(2*a);
printf("x1=%.2lf\n", x1);
printf("x2=%.2lf\n", x2);
}

//condition for real and equal roots
else if (d == 0);
{
x1 = -b/(2*a);
printf("x1=x2=%.2lf", x1);
}

// if roots are not real
else (d < 0);
printf("math error, do your own job boi!!!");

Why it said there are errors on "else if" and "else"?

answered Jan 20, 2019 by anonymous
it said " error: 'else' without a previous 'if' on both lines...
commented Apr 26, 2019 by anonymous
this is one of the most common errors in c programming, and the best cause is to buy a book or look at older programs that have working if else statements
answered Jan 20, 2019 by (140 points)
there should not be ; after if(....);

else if(......);

and else (d<0) is not right it should be else
commented Jan 20, 2019 by Question Man
Thank you so much!!!
answered Jan 21, 2019 by the_one
u can't put conditions on else function like in if() and else if()

just put without those conditions
answered Jan 21, 2019 by (140 points)
power cant be return as x^2

math.h func should be used
+1 vote
answered Jan 21, 2019 by (160 points)

/*Dont use ; after if and else,
dont write condition for else, it automatically use a condition according to just privious if ,
dont forget to write condition for if in (),
if there is more than 1 statement write in {}
*/

#include<stdio.h>

#include<math.h>

int main()

{

double a, b, c, d, x1, x2;
printf("ax^2+bx+c=0\n");
printf("a=\n");
scanf("%lf", &a);
printf("b=\n");
scanf("%lf", &b);
printf("c=\n");
scanf("%lf", &c);
d = b*b-4*a*c;
// condition for real and different roots
if (d > 0)
{
// sqrt() function returns square root
x1 = (-b+sqrt(d))/(2*a);
x2 = (-b-sqrt(d))/(2*a);
printf("x1=%.2lf\n", x1);
printf("x2=%.2lf\n", x2);
}

//condition for real and equal roots
else
{

if (d==0)

{
x1 = -b/(2*a);
printf("x1=x2=%.2lf", x1);
}

// if roots are not real
else
printf("math error, do your own job boi!!!");

}

return 0;

}

answered Jan 22, 2019 by anonymous
you should not end if and else if with ";"
answered Feb 3, 2019 by (150 points)
edited Feb 3, 2019 by Kritik Seth

You cannot add semi colon after if, else if and else statements.

This is a little more complex version of the same code, it takes into account when a and/or b are 0 and the formula changes (the equation becomes linear)

#include<stdio.h>

#include<math.h>

int main()

{

float a,b,c;

float r1,r2,r3,r11,r12,r21,r22,D;

printf("Enter values of a,b,c where equation is ax^2+bx+c:\n");

scanf(" %f %f %f",&a,&b,&c);

if(a==0 && b==0)

{

printf("Invalid Input, a and b cannot be %f and %f\n",a,b);

}

else if(a==0 && b!=0)

{

printf("Linear Equation\n");

r3=(-c/b);

printf("The roots of %.1fx^2 + %.1fx + %.1f is %.3f\n",a,b,c,r3);

}

else if(a!=0 && b!=0)

{

D=((b*b)-(4*a*c));

if(D>0)

{

r1=((-b+sqrt(D))/2*a);

r2=((-b-sqrt(D))/2*a);

printf("The roots of %.1fx^2 + %.1fx + %.1f are %.3f and %.3f\n",a,b,c,r1,r2);

}

else if(D<0)

{

r11=(-b)/(2*a);

r12=sqrt(-D)/(2*a);

r21=(-b)/(2*a);

r22=sqrt(-D)/(2*a);

printf("The roots of %.1fx^2 + %.1fx + %.1f are %.3f + %.3fi and %.3f - %.3fi",a,b,c,r11,r12,r21,r22);

}

}

}