Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (2.6k points)

If I want to find the sum of the digits of a number, i.e.:

  • Input: 932
  • Output: 14, which is (9 + 3 + 2)

What is the fastest way of doing this?

1 Answer

0 votes
by (7.2k points)

sum the digits of a number in Python.

def sum(n): 

s = 0 while n: 

s += n % 10 n 

//= 10 return s

Related questions

0 votes
1 answer
asked Dec 17, 2020 in Python by ashely (50.2k points)
0 votes
1 answer
asked Feb 15, 2021 in Java by sheela_singh (9.5k points)
0 votes
1 answer
asked Aug 12, 2019 in Java by Anvi (10.2k points)
0 votes
1 answer

Browse Categories

...