diff --git a/maths/sum_of_digits.py b/maths/sum_of_digits.py new file mode 100644 index 000000000..88baf2ca2 --- /dev/null +++ b/maths/sum_of_digits.py @@ -0,0 +1,18 @@ +def sum_of_digits(n: int) -> int: + """ + Find the sum of digits of a number. + + >>> sum_of_digits(12345) + 15 + >>> sum_of_digits(123) + 6 + """ + res = 0 + while n > 0: + res += n % 10 + n = n // 10 + return res + + +if __name__ == "__main__": + print(sum_of_digits(12345)) # ===> 15