The first thing we need to do is figure out how to get each digit. Two ways:
1. Convert the number to a string (using std::to_string()) and each index of the string corresponds to a digit of the number (as a char which would be a really nice solution). I have a feeling that you are expected to use option 2 (or a variation of it),
2. Use the modulo operator (%) to obtain each digit (as an int), convert that number to a char and print the ascii value.
Okay, so we will look at option 2. Modulo returns the remainder after an integer division (so 3 % 2 is equal to the remainder of the division operation 3 / 2 which is 1).
This means that given an integer (n) in base (b), the rightmost digit (int base ten, this would be the ones place) is equal to n % b. Example:
123 / 10 is, as we know, 12 because of integer division. We also know that there is a remainder (r) of 3 (12 * 10 + r= 123; 120 + r = 123; r = 123 - 120; r = 3).
Now, if we take our number and mod it by the base (from this point on I will assume we are working in base 10) and get the first digit, as demonstrated above, we can divide by 10 and "chop off" that digit. Now, if we continue doing these steps until the number is 0 (hint: think about loops), we can obtain each digit (starting from the right of course).
yes ninja, your algorithm is as Danny suggested, unfortunately it works in reverse order.
you could store the results in an array and print them out in reverse order.
or, because it is only 4 digits, take them explicitly....
1 2 3 4 5 6 7 8
int thousands = number / 1000;
number = number - thousands;
int hundreds = number / 100;
number = number - hundreds;
etc...
write ascii(thousands), ascii(hundreds)...