// program to print the sum of the digits in an entered number. #include using namespace std; int main() { int number; int sum; int digit; cout << "Enter a positive number -- "; cin >> number; cout << "The sum of the digits of " << number << " is "; // use remainder and divide to get the digits one at a time and sum them // Note the last digit can be obtained by finding the remainder when // the number is divided by 10. We can then divide the number by 10 to // remove the last digit. We will repeat this until the number is 0 sum = 0; while (number > 0) { digit = number % 10; sum = sum + digit; number = number / 10; } cout << sum << endl; return 0; }