After running code, the output is always zero

You just declare the variable x, but not initialize it. So it causes undefined behavior.

int x,i; // <-- you declare the variable, but you didn't assign a value to x

Then, you assign result to x:

double y, result= x; // so now result is undefined behavior

Only now you give x a value by: scanf("%d %lf",&x,&y); but it’s too late. result is now undefined behavior.

You should change your code to this:


int main()
    int x,i;
    double y;
    scanf("%d %lf",&x,&y); // <-- now you give x a value
    result= x; // <-- so now result is not undefined behavior anymore.

    for(i=0; i<3; i++)
        result = result+(result*y);

    return 0;   

Browse More Popular Posts

Leave a Comment