proconlib

This documentation is automatically generated by competitive-verifier/competitive-verifier

View the Project on GitHub anqooqie/proconlib

:heavy_check_mark: Sum of digits (tools/digit_sum.hpp)

template <typename T>
T digit_sum(T n);

It returns sum of the digits of $n$.

Constraints

Time Complexity

License

Author

Verified with

Code

#ifndef TOOLS_DIGIT_SUM_HPP
#define TOOLS_DIGIT_SUM_HPP

#include <cassert>

namespace tools {

  template <typename T>
  T digit_sum(T n) {
    assert(n >= 0);
    T sum = 0;
    for (; n > 0; n /= 10) {
      sum += n % 10;
    }
    return sum;
  }
}

#endif
#line 1 "tools/digit_sum.hpp"



#include <cassert>

namespace tools {

  template <typename T>
  T digit_sum(T n) {
    assert(n >= 0);
    T sum = 0;
    for (; n > 0; n /= 10) {
      sum += n % 10;
    }
    return sum;
  }
}


Back to top page