Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

What's wrong with my c prog? Please help!

+2 votes
asked Jan 20, 2019 by Question Man
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"?

8 Answers

0 votes
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
0 votes
answered Jan 20, 2019 by Shrikant Dalmia (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!!!
0 votes
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
0 votes
answered Jan 21, 2019 by Rushil (140 points)
power cant be return as x^2

math.h func should be used
+1 vote
answered Jan 21, 2019 by Mukesh_the_noughty_guy (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;

}

0 votes
answered Jan 22, 2019 by anonymous
you should not end if and else if with ";"
0 votes
answered Feb 3, 2019 by Kritik Seth (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);

        }

    }

}   

0 votes
answered Feb 4, 2019 by anonymous
yah u did some errors so u ncould learn first c language the next u do it will c ome exactyy
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...