CSCI 151 Introduction to Computer Science

Sum Digits Algorithm

Input: 3-digit non-negative integer

Output: The sum of the digits of the given input number

1. Denote the input number by number and the output by sum.
2. Initialization: number = 0; sum = 0.
3. Extract last digit (units) and leave the number with two other digits
4. Add the extracted digit to the sum .
5. Extract middle digit (tens) and leave the number with the last digit
6. Add the extracted digit to the sum .
7. Extract the first digit (hundreds) and add it to the sum .
8. Print the result.