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");
    }

    else
    {
        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.

I see one major problem here

int x;
scanf("%lf", &x);

Your treating x like its a float.

Changed it to
double x;

Works now, appreciate it!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.