-24

In C++, I have the following input: 12345

How can I achieve this output: 1 2 3 4 5 ?


Another example could be:

input: 123

output: 1 2 3
SherylHohman
  • 16,580
  • 17
  • 88
  • 94

1 Answers1

3

You can simply do this:

int num = 123;
std::vector<int> digits;

while( num > 0 ) {
    digits.push_back(num % 10);
    num /= 10;
}
DimChtz
  • 4,043
  • 2
  • 21
  • 39
  • @PriteshJadhav Using something like `push_back(digit)` instead of `cout << digit` is an enormous intellectual act, I absolutely agree. – πάντα ῥεῖ Jul 17 '16 at 16:59
  • _@DimChtz_ I actually hesitate to up- or downvote your answer. On the one hand I don't think it's a good custom to answer off-topic questions. On the other hand I want to hinder the OP deleting that question themselves, but letting it go fully processed. – πάντα ῥεῖ Jul 17 '16 at 17:22