From 0904610a7671e52b164157ef5d7e0cc54d75399b Mon Sep 17 00:00:00 2001 From: Vignesh Date: Wed, 3 Jun 2020 03:08:32 +0530 Subject: [PATCH] create sum_of_digits.py (#2065) * create sum_of_digits.py create sum_of_digits.py to find the sum of digits of a number digit_sum(12345) ---> 15 digit_sum(12345) ---> 10 * Update sum_of_digits.py * Update maths/sum_of_digits.py Co-authored-by: Christian Clauss * Update maths/sum_of_digits.py Co-authored-by: Christian Clauss * Update sum_of_digits.py Co-authored-by: Christian Clauss --- maths/sum_of_digits.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 maths/sum_of_digits.py 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