Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

how to sum digits of a number in python

number = 123 # the number you want summed up
sum_of_digits = 0 
for digit in str(number):
  sum_of_digits += int(digit)
  
print(sum_of_digits) # printing the final sum of number
Comment

how to find the sum of digits of a number in python

num = int(input("Enter the number: "))
sum_of_digits = 0
while num > 0:
    digit = num % 10
    num //= 10
    sum_of_digits += digit
print("The sum of digits is", sum_of_digits)
Comment

sum of number digits python

num = input("Enter your number: ")
result = 0
for n in str(num):
        result += int(n)
print("Result: ",result)

#code by fawlid
Comment

python Program for Sum of the digits of a given number

# Python 3 program to
# compute sum of digits in
# number.
 
# Function to get sum of digits
 
 
def getSum(n):
 
    sum = 0
    while (n != 0):
 
        sum = sum + int(n % 10)
        n = int(n/10)
 
    return sum
 
 
# Driver code
n = 687
print(getSum(n))
Comment

sum of digits in python

# Python program to
# compute sum of digits in 
# number.
   
# Function to get sum of digits 
def getSum(n):
     
    strr = str(n)
    list_of_number = list(map(int, strr.strip()))
    return sum(list_of_number)
   
n = 12345
print(getSum(n))
Comment

PREVIOUS NEXT
Code Example
Python :: what does class meta do in django 
Python :: Reverse an string Using Recursion in Python 
Python :: how to hide a widget in tkinter python 
Python :: create and populate dictionary python 
Python :: instabot login python 
Python :: merge two Python dictionaries in a single expression 
Python :: how store list in django session 
Python :: accept user input in python 
Python :: np arange shape 
Python :: django createmany 
Python :: format list into string python 
Python :: python how to convert csv to array 
Python :: python print on file 
Python :: python send http request 
Python :: pygame how to draw a rectangle 
Python :: python class variables make blobal 
Python :: matplotlib savefig not working 
Python :: fillna with mode pandas 
Python :: how to resize tkinter window 
Python :: df.iterrows() 
Python :: clamp number in python 
Python :: pandas count unique values in column 
Python :: horizontal bar plot matplotlib 
Python :: select non nan values python 
Python :: chrome driver in python selenium not working 
Python :: extract data from json file python 
Python :: tensorflow_version 
Python :: how to add two matrix using function in python 
Python :: change string list to int list python 
Python :: python request response json format 
ADD CONTENT
Topic
Content
Source link
Name
8+3 =