Say I have a multi-digit integer in C. I want to break it up into single-digit integers.
123
would turn into 1
, 2
, and 3
.
How can I do this, especially if I don't know how many digits the integer has?
Say I have a multi-digit integer in C. I want to break it up into single-digit integers.
123
would turn into 1
, 2
, and 3
.
How can I do this, especially if I don't know how many digits the integer has?
This results in the following printout:
Hundreds = 9
Tens = 8
Units = 7
As a hint, getting the nth digit in the number is pretty easy; divide by 10 n times, then mod 10, or in C:
First, count the digits:
Then, you can store them in an array:
I think below piece of code will help....
You can use %10, which means the remainder if the number after you divided it. So
123 % 10
is 3, because the remainder is 3, substract the 3 from 123, then it is 120, then divide 120 with 10 which is 12. And do the same process.