Python :Sum of digits eg #2


SUBMITTED BY: Guest

DATE: Dec. 16, 2013, 8:42 a.m.

FORMAT: Python

SIZE: 387 Bytes

HITS: 804

  1. # find sum of digits
  2. n = input("enter an integer : ")
  3. # slicing : similar to range
  4. # always [] while indexing or slicing on any data structure
  5. #print (n[2]) # indexing
  6. #print (n[2:4]) # slicing
  7. #print (n[2:])
  8. #print (n[:4])
  9. #print (n[2:6:2])
  10. print(type(n))
  11. i = 0
  12. total = 0
  13. while i < len(n) :
  14. total += int(n[i])
  15. i += 1
  16. print(total)

comments powered by Disqus