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:

#include<stdio.h>

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++)
    {
        y/=100;
        result = result+(result*y);
    }

    printf("%.2lf\n",result);
    return 0;   
}

Leave a Comment