September 29th, 2009, 10:35 PM
Homework Help! Probably very easy...
I have been doing this homework since the day it was assigned and I have 3 problems left out of 11 with absolutely no clue on how to do them.
Please help me its due tomorrow!
Here's one of them..
It wants me to write a program that prompts the user to input an integer and then output both the individual digits of the number and the sum of the digits. For example, it should output the individual digits of 3456 as 3 4 5 6, output the individual digits of 8030 as 8 0 3 0, output the individual digits of 2345526 as 2 3 4 5 5 2 6, output the individual digits of -2345 as 2 3 4 5.
It seems like it would be very easy but I just can't seem to come up with what to do and my professor won't pick up his phone! LOL =[
September 29th, 2009, 11:40 PM
Re: Homework Help! Probably very easy...
There are two ways to approach this sort of problem.
1) Use modular math to identify each individual digit sequentially, eg, n % 10 is the ones digit of n.
2) Convert the integer to a string (multiple approaches---stringstream, itoa, sprintf, etc) and then just process the resulting string. Be careful to recognize the difference between the character '1' and the number 1 when adding the digits, though!
Tags for this Thread
Click Here to Expand Forum to Full Width