How to get the digits of a number without converting it to a string/ char array?

前端 未结 15 1284
既然无缘
既然无缘 2020-11-27 04:07

How do I get what the digits of a number are in C++ without converting it to strings or character arrays?

15条回答
  •  Happy的楠姐
    2020-11-27 04:50

    Use a sequence of mod 10 and div 10 operations (whatever the syntax is in C++) to assign the digits one at a time to other variables.

    In pseudocode

    lsd = number mod 10
    number = number div 10
    next lsd = number mod 10
    number = number div 10
    

    etc...

    painful! ... but no strings or character arrays.

提交回复
热议问题