Practice Exercise
Filename: Digits.java
- Write a method called SumDigits that takes in an
integer parameter and computes and returns the sum of its digits. If
the
incoming integer is negative, then disregard the negative (and still
compute the sum of the digits, as a positive result).
- This method should not depend on any LIMIT to the number of
digits. Assume no limit to how many digits a number can have (remember
the range of an int depends on the computer architecture -- it differs
for different machines -- the method should work for any number of
digits).
- To test this method, write a main() routine that enters a
loop, in which the user is prompted and allowed to enter any integer (0
to
exit the loop), and the integer is sent to the method and the result
printed. The user should keep being asked for inputs until they type a
0
to exit.
Sample Run
(user input underlined)
Please enter an integer (0 to quit): 124566
The sum of the digits of 124566 is 24
Please enter an integer (0 to quit): 23453245
The sum of the digits of 23453245 is 28
Please enter an integer (0 to quit): 45454545
The sum of the digits of 45454545 is 36
Please enter an integer (0 to quit): -9189845
The sum of the digits of -9189845 is 44
Please enter an integer (0 to quit): 0
Goodbye