Python Tutorial #8 :Sum of digits


SUBMITTED BY: Guest

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

FORMAT: Python

SIZE: 293 Bytes

HITS: 825

  1. # find sum of digits
  2. # C pgm in Python
  3. # 2.7 : raw_input
  4. n = input("enter an integer : ")
  5. print(n, type(n))
  6. n = int(n)
  7. print(n, type(n))
  8. total = 0
  9. i = 0
  10. while n :
  11. total += n % 10
  12. n //= 10
  13. i += 1
  14. print(total)
  15. print("# of iterations : ", i)

comments powered by Disqus