what would be a good way to go about summing the digits of an int for example say i put in 3456 the sum of thos would be 18 but i have no idea how to get the numbers to sum from a single integer
Ryan61343
0
Newbie Poster
Recommended Answers
Jump to PostSplit them up using the modulus operator %.
Jump to PostI would use std::stringstream to put the int into a stirng. Then the logic is much more straight forward to get each digit of the string. You can again use stringstream to get each one back to an int before adding them.
iamthewee's option also works, but it could …
All 6 Replies
iamthwee
daviddoria
334
Posting Virtuoso
Featured Poster
Ryan61343
0
Newbie Poster
tesuji
135
Master Poster
Ryan61343
0
Newbie Poster
Ryan61343
0
Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.