Here is my code to count the sum of 5 and 1 coin of a certain money to be the less. But I have to assign variable c to variable v because if not do so, the program will stop if the money is less than 5 in the first place. Can anyone tell me how I can get the value of variable c after a loop without assign it to another variable?

int c;
int i;
int k;

    c = get_int ("c: ");
} while (c < 0);

for (i = 0; c >= 5 ; i++)
    c = c - 5;

int v = c;
for (k = 0 ; c >= 1 ; i++)
    v = v -1 ;

printf("%i\n", i + k);

1 Answer 1


The problem is that in your second for loop, you are reducing c, then setting v = c then your next loop condition is on c but your loop is altering v and the for condition is incrementing i again instead of k so you will have an infinite loop and k will never not equal 0.

no need to create variable v, just use c,
the last for loop should be

for (k = 0, c > 0; k++ )
    c = c -1;

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .