Algorithmen_Datenstrukturen/Uebung 4/Uebung4_3/digitSum.cpp

18 lines
531 B
C++

#include <iostream>
#include <string>
/* algorithm that "hashes" the through digit sum. The size needed for
* this would be 64, since telephone numbers are exactly 7 digits long and the
* maximum value a digit can have is 9 -> 7*9 = 63, plus 1 since we start at
* zero. */
int digitSum(std::string telNr) {
uint32_t number = std::stoi(telNr);
uint8_t digitSum = 0;
while (number > 0) {
digitSum += number % 10;
number /= 10;
}
return digitSum;
}
int main() { std::cout << digitSum("9797979") << std::endl; }