cplusplus
.com
TUTORIALS
REFERENCE
ARTICLES
FORUM
C++
Tutorials
Reference
Articles
Forum
Forum
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Lounge
Jobs
Forum
Beginners
Question about Numbers Addition?
Question about Numbers Addition?
Oct 22, 2011 at 12:02am UTC
Fariseagle
(5)
How do I add the numbers in 1 number together?
Ex. 1925 = 1+9+2+5 = 17 then 1+7 = 8
Oct 22, 2011 at 4:27am UTC
hamsterman
(4538)
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?
Oct 22, 2011 at 5:50am UTC
Fariseagle
(5)
Yes, Thank you, but is there a way to get a middle number like in 12345 which is "3"?
Oct 22, 2011 at 7:32am UTC
hamsterman
(4538)
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.