Starting with the code below, implement the sumDigits() function that calculates the sum of all the digits in an integer. You may assume that the inputs (x) will always be 0 or a positive integer.
void setup() {
Serial.begin(9600);
Serial.println(sumDigits(0)); // Should be 0
Serial.println(sumDigits(2)); // Should be 2
Serial.println(sumDigits(28)); // Should be 10
Serial.println(sumDigits(504)); // Should be 9
Serial.println(sumDigits(2048)); // Should be 14
Serial.println(sumDigits(32767)); // Should be 25
}
void loop() {
// Do nothing
}
int sumDigits(int x) {
// YOUR CODE GOES HERE
}
Hints:
When you run the program, you should see the following output:
