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?
I made this based on the code from @asaelr:
It works quite well, thanks!
You can divide and conquer but you have rewrite all of arithmetic libraries. I suggest using a multi-precision library https://gmplib.org But of course it is good practice
we can use this program as a function with 3 arguments.Here in "while(a++<2)", 2 is the number of digits you need(can give as one argument)replace 2 with no of digits you need. Here we can use "z/=pow(10,6)" if we don't need last certain digits ,replace 6 by the no of digits you don't need(can give as another argument),and the third argument is the number you need to break.
The last digits of 123 is 123 % 10. You can drop the last digit of 123 by doing 123/10 -- using integer division this will give you 12. To answer your question about "how do I know how many digits you have" -- try doing it as described above and you will see how to know when to stop.