I ran it through an IDE and the remainder values came out 3, 2, 0, 1. I understand the first remainder, but not the rest. Also, how come the loop terminates? Isn't x always going to be greater than 0, therefore continuing indefinitely? Thank you.
int x = 1023;
while (x > 0)
{
printf("%d", x% 10);
x = x /10;
}
xis divided by10in each loop. This is equivalent to shifting the decimal mumber one place to the right, and the loop stops when there is nothing left. Notice the pattern of the output - it is the number in reverse order.x = x /10;this is an integer divide. In such a divide, all fractions are dropped. so the results of that divide are: 102, 10, 0 the modulo results are 1023%10= 3, 102%10= 2, 10%10= 0, etc