've done the code but I have a problem.... instead of adding all the integers and returning the answer, the program instead returns the last intetger as the answer.
Note that Punit Jain gave you a hint about what might be wrong: carefully look at the condition for the while loop in line 11: while (number >= 100) - is that what you intended? Suppose the user enters 234, then what is the program going to do exactly? Trace the steps the program will take - that will make it clear why it doesn't do what you think it should do.
fred rosenberger wrote:
all programming is done the same way. You break the problem down into very simple parts, and work on ONE at a time.
For example, This program requires you to to at LEAST four things:
1) Get input from a user
2) convert a number into into individual digits.
3) Sum a bunch of numbers
4) print out a number
It is key to realize that each and every one of those can - and more importantly should - be done separately from the others.
So, work on one at a time. Only when you have gotten whichever one you do first to work should you start to think about working on the second.
Punit Jain wrote:
while (number >=100)
sum = sum+counter;
Trace your code and check your condition in while loop.