This week's book giveaway is in the Servlets forum. We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line! See this thread for details.

This will work as long as you get numbers between 0 and 1000. What will you do if your requirements change, and you should do the same with numbers between 0 and 10.000? And then with numbers between 0 and 100.000? And so on?

One tip: you can do this with a loop and solve it for all kinds of numbers. I'll show you how with your example.

Start: number left = 432, sum = 0
Iteration 1: number left = 43, sum = 2
Iteration 2: number left = 4, sum = 2 + 3
Iteration 3: number left = 0, sum = 2 + 3 + 4
Nothing more to add so stop the loop.

Let's now use a larger number:
Start: number left = 987654321, sum = 0
Iteration 1: number left = 98765432, sum = 1
Iteration 2: number left = 9876543, sum = 1 + 2
...
Iteration 8: number left = 9, sum = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8
Iteration 9: number left = 0, sum = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9
Nothing more to add so stop the loop.

While your code is fine, you asked for a code review - so two tips:

1) You don't appear to use x3 and x4 so I would recommend removing them. If this were a real program that someone had to maintain, it would be clearer.
2) I would name x1 "firstDigit" or something that made it clearer what it is.

@Rob Prime: Thanks for the tip! Not at my house at the moment. But i'll work on the code as soon as i arrive. I think i'll try the for loop on this one.

@Jeanne Boyarsky: Thanks! i completely forgot about x3 and x4.. LOL, as for naming my variables, you do have a point. Thanks

subject: Please critic, rate or give some helpful advice