Thread: quadratic equation

  1. #1
    Registered User
    Join Date
    Apr 2017
    Posts
    12

    quadratic equation

    Hi. the return values are not coming out properly. Any reasons why would be a big help.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    float b;
    float a;
    float c;
    float z;
    
    
    int main()
    {
        printf("please enter a, b and c \n");
        scanf("%f", &a);
        scanf("%f", &b);
        scanf("%f", &c);
    
    
        z= (-b+ sqrt((b*b)-4*a*c))/2*a;
        z= (+b+ sqrt((b*b)-4*a*c))/2*a;
        printf("%f \n");
        printf("%f \n");
    }

  2. #2
    Registered User
    Join Date
    Feb 2012
    Posts
    347
    Missing math.h header. Printf not used properly.

  3. #3
    Registered User
    Join Date
    Apr 2017
    Posts
    12
    Ok I put the header in and changed the printf but still doesn't work.

  4. #4
    Registered User
    Join Date
    Feb 2012
    Posts
    347
    Oops Latest code and what is the input you are giving what is the expected and actual output. Can you provide.

  5. #5
    Registered User
    Join Date
    Apr 2017
    Posts
    12
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    float b;
    float a;
    float c;
    float z;
    float r;
    int main()
    {
        printf("please enter a, b and c \n");
        scanf("%f", &a);
        scanf("%f", &b);
        scanf("%f", &c);
        z= (-b+ sqrt((b*b)-4*a*c))/2*a;
        r= (+b+ sqrt((b*b)-4*a*c))/2*a;
        printf("%f \n",z);
        printf("%f \n",r);
    }
    quadratic equation-gxe61pu-jpg

  6. #6
    Registered User
    Join Date
    Feb 2012
    Posts
    347
    Make sure that value in sqrt is positive and 2*a should be in brackets.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Your implementation of the formula is wrong. For example, this:
    Code:
    z= (-b+ sqrt((b*b)-4*a*c))/2*a;
    should have been:
    Code:
    z = (-b + sqrt(b * b - 4 * a * c)) / (2 * a);
    or if you prefer to use parentheses to be more explicit about the grouping:
    Code:
    z = (-b + sqrt((b * b) - (4 * a * c))) / (2 * a);
    Look very carefully at:
    Code:
    r= (+b+ sqrt((b*b)-4*a*c))/2*a;
    compare closely with the mathematical expression that you are trying to implement.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Apr 2017
    Posts
    12
    Thank you for the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quadratic equation
    By High Voltage in forum C Programming
    Replies: 3
    Last Post: 01-04-2016, 01:11 PM
  2. Quadratic Equation
    By joboo in forum C Programming
    Replies: 5
    Last Post: 10-18-2013, 03:28 AM
  3. quadratic equation
    By nynicue in forum C Programming
    Replies: 5
    Last Post: 12-26-2008, 05:57 PM
  4. Quadratic Equation
    By Lucid15 in forum C Programming
    Replies: 20
    Last Post: 09-16-2008, 05:59 PM
  5. Quadratic Equation problems
    By Rachel228 in forum C++ Programming
    Replies: 2
    Last Post: 03-15-2006, 02:42 AM

Tags for this Thread