+ 2

Divide

how we can divide numbers in c++? example: 12345 => 1,2,3,4,5

11th Feb 2018, 3:23 PM
Daniel Fathi
4 Antworten
+ 2
https://stackoverflow.com/questions/4261589/how-do-i-split-an-int-into-its-digits "Given the number 12345 : 5 is 12345 % 10 4 is 12345 / 10 % 10 3 is 12345 / 100 % 10 2 is 12345 / 1000 % 10 1 is 12345 / 10000 % 10 I won't provide a complete code as this surely looks like homework, but I'm sure you get the pattern."
11th Feb 2018, 3:42 PM
Alex
Alex - avatar
+ 2
This won’t work on SoloLearn because of glitches with the compiler, but anywhere else, you could convert to string then do it that way: int num = 12345; std::string str = std::to_string(num); cout << str[4] << str[3] << str[2] << str[1] << str[0]; //outputs 54321
12th Feb 2018, 12:35 AM
Jacob Pembleton
Jacob Pembleton - avatar
0
Thanks guys ;)
22nd Mar 2018, 6:46 PM
Daniel Fathi