Ok so, I'm working on a program that is suppose to solve a function:

(1+ (x^2) / sqrt(3x+6)) / (x^2 - 5x)

As you tell by the function the denominator cannot be = to 0.

So here's my code that I'm having trouble with.

#include <stdio.h>
#include <math.h>

int main(void)
    int x;
    printf("Enter a number x: ");
    scanf("%lf", &x);

    double d = ((x*x)-(5*x));

    if (d == 0)
        printf("f(x) is not defined\n");

        double n = 1 + ((x*x)/sqrt(3*x+6));
        double f = n/d;
        printf("f(x) = %lf\n", f);

    return 0;

It seems to be doing everything that it's suppose to, but its not unfortunately. So basically, the program should being by calculating the denominator. If the denominator returns as 0, then the program prints that the function is not defined. If denominator is not 0, then it continues to solve the function.

However, when I run the program the answers come out incorrect.

For example, when I type in 2.75 for my value x I would get "f(x) is not defined" Even though for the value 2.75 does not equal 0.

For the value 2.74, I would get -.00011 which is wrong. The answer should be -0.48299.

I'm not sure what I am doing wrong. It seems very random. Certain values will come back as not defined when the denominator does not equal zero. Or certain values will come back entirely wrong.

6 Years
Discussion Span
Last Post by MrNo
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.