user239321 user239321 - 2 months ago 134
C Question

I can't understand what's going wrong in this program

#include<math.h>
#include<stdio.h>
int main(void)
{
int i = 0;

int f = 10000;

int div1 = (powl(10,i));

int temp1 = f/div1;

for(i = 2; temp1 > 1; i++)
{
printf("%i\n",temp1);
}
}


As far as I know, the value of div1 should be 100,1000,10000... With corresponding increments in I. Then temp1 should be 100,10, then loop stops (?). But I get an endless loop of 10000 10000 10000 10000......

Can someone explain what am I doing wrong?

Answer Source

Your for statement should have like this. You missed to call those to statement inside your for loop

    for(i = 2; temp1 > 1; i++)
    {
        div1 = (powl(10,i));

        temp1 =  f/div1;
        printf("%i\n",temp1);

    }