a little c++ programm

Hello. How I can create this programm: "The user enter a natural number. Ascertain, how much digits are in this number and digit's sum.

Thanks a lot!
There are two approaches, both of which are heavily documented around these forums as it is a common
homework assignment. I'll give you a general overview of the simpler approach, and then you can do some
independent research.

Use std::cin to read in an unsigned integer. The result of any integer % 10 is the value of the ones digit.
The result of any integer / 10 is the value of the remaining digits. For example, 1234 % 10 == 4 (the ones
digit) and 1234 / 10 == 123 (the remaining digits). If you repeat the algorithm on the remaining digits (123),
you get 123 % 10 == 3 and 123 / 10 == 12. So now you have 3, which is the second to last digit. To get
the sum of the digits, you simply add up all the modulo remainders. You should use a loop to repeat the
above process until some terminating condition, which I'll let you think about. The number of times the loop
iterated is the number of digits in the number.

Thanks, it help! :)
Topic archived. No new replies allowed.