Quadratic equation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ginevralupin
    New Member
    • Apr 2008
    • 6

    #1

    Quadratic equation

    hi
    i m a student nd need to write a program to input coefficients of a quadratic equation and output its roots .
    There is some problem in this , i tried several variations but it is not working.
    Please check and let me know what's wrong and how to fix it...
    #include <stdio.h>
    #include <conio.h>
    #include <math.h>

    main()
    {
    float a,b,c,d,r1,r2;

    printf("Enter the coefficients ...");
    scanf("%f %f %f",&a,&b,&c) ;

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

    if (!d<0)
    {
    if (d=0)
    {
    r1=((-b)/(2*a));
    r2=r1;
    }
    else
    // if (d>0)
    {
    r1=((-b+sqrt(d))/(2*a));
    r2=((-b-sqrt(d))/(2*a));
    }

    }

    printf("%f %f",r1,r2);

    getch();
    return 0;

    }
  • Savage
    Recognized Expert Top Contributor
    • Feb 2007
    • 1759

    #2
    This:

    [CODE=cpp]if (d=0)[/CODE]

    is your problem.You are using assigment operator instead of eqaulity operator which is ==

    Also don't use main(),use int main becasue that's what standard says you should use.

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      ... and this:

      [code=c]
      if (!d<0)
      [/code]

      evaluates as if you had written this:

      [code=c]
      if((!d) < 0)
      [/code]

      both (see the previous reply) are nonos.

      kind regards,

      Jos

      Comment

      • ginevralupin
        New Member
        • Apr 2008
        • 6

        #4
        thnx so much,it works now !

        Comment

        Working...