Im a CSE fresher. I found this code on w3school and dont know whats going on.
#include <stdio.h>
int main() {
// A variable with some specific numbers
int numbers = 12345;
// A variable to store the reversed number
int revNumbers = 0;
// Reverse and reorder the numbers
while (numbers) {
// Get the last number of 'numbers' and add it to 'revNumber'
revNumbers = revNumbers * 10 + numbers % 10;
// Remove the last number of 'numbers'
numbers /= 10;
}
// Output the reversed numbers
printf("%d", revNumbers);
return 0;
}
Can anyone explain the loops condition?
numbers % 10
do. If you do, fine. If you didn't, that is the remainder operator.n % 10
gives you the remainder of the divisionn / 10
. For example, ifn =12345
,n / 10 = 1234
(n
is anint
, so there is no decimal part) and the remainder of that division is 5 (n % 10 = 5
). If you already knew that, sorry for the post.