Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reuf/c500274129ddb4b51bd92b8cc072e1fd to your computer and use it in GitHub Desktop.
Save reuf/c500274129ddb4b51bd92b8cc072e1fd to your computer and use it in GitHub Desktop.
# https://www.w3resource.com/python-exercises/python-basic-exercises.php
# 68. Write a Python program to calculate the sum of the digits in an integer.
def sumOfDigsInInt(integer):
sum = 0
# Method 1:
# counter = integer
# while counter > 0:
# if int(integer/10) > 0:
# sum += integer % 10
# integer = int(integer/10)
# else:
# sum += integer
# break
# Method 2
# numSize = len(str(integer)[:])
# print("Int has %d digits" % numSize)
# for i in range(numSize):
# sum += integer % 10
# integer = int(integer/10)
# Method 3
for i in str(integer)[:]:
sum += int(i)
return sum
num = int(input("Input an integer: "))
print("Sum of individual digits within that intger is = ",sumOfDigsInInt(num))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment