-25

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
  • 14,460
  • 16
  • 79
  • 88

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
  • 3,692
  • 2
  • 16
  • 36
  • @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