Created
October 18, 2019 07:19
-
-
Save reuf/c500274129ddb4b51bd92b8cc072e1fd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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