Question about Numbers Addition?

How do I add the numbers in 1 number together?


Ex. 1925 = 1+9+2+5 = 17 then 1+7 = 8
To get the last digit of x, write x % 10. To get everything but the last digit, x / 10. Can you see how to play with these two to write a loop that gets all the digits?
Yes, Thank you, but is there a way to get a middle number like in 12345 which is "3"?
1
2
int a = 12345;
cout << a / 100 % 10;

or, using what I told you
1
2
3
a = a/10;
a = a/10;
cout << a % 10;

this way you can easily put it in a loop.
Topic archived. No new replies allowed.