this is straightforward, but may include a slight twist first.
I am not going to give you the actual code, but some piece that, if you can understand them, you have to solve this problem yourself:
What you want to do is convert the integer to the string / char. The most important thing you need to do is convert one int digit to the corresponding char.
what you need to do by adding me to '0'. The reason this works is because '0' actually means an integer value of 48. '1' .. '9' means 49..57. This is a simple addition to define the appropriate character for an integer with a decimal digit:
i.e. char c = '0' + i;
If you know how to convert one decimal int to char, then what remains is how you can extract a single digit from integers with more than one decimal.
it's just simple math using / and%
int i = 123 % 10; // give u last digit, which is 3 int j = 123 / 10; // give remove the last digit, which is 12
The logic on the left is the homework you need to do.
source share