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

python sum of digits in a string

# Here is the short version
number = 159
sum((int(n) for n in str(abs(number))))
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 adding digits

def sum_digits(num: int) -> int:
	
	#base case when your "positive" integer get to 0
    if num == 0: 
        return 0
    #base case when your "negative" integer get is between -10 and 0
    if num > -10 and num < 0:
        return num
	
	# recursion for positive integer
    elif num > 0:
        return (num % 10) + sum_digits(num//10)
	
	# recursion for negative integer
    elif num < 0:
        return -(abs(num) % 10) + sum_digits(-(abs(num)//10))
      
sum_digits(123) # returns 6
sum_digits(-123) # returns -6
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 :: how to read pdf in python 
Python :: linear search in python 
Python :: pip neat 
Python :: how to get user location in python 
Python :: matplotlib 3D plots reduce margins 
Python :: how to loop the length of an array pytoh 
Python :: seaborn hue order 
Python :: remove single and double quotes from string python 
Python :: load saved model 
Python :: remove negative numbers from list python 
Python :: python extract specific columns from pandas dataframe 
Python :: text to speech python 
Python :: python for loop jump by 2 
Python :: change background color of tkinter 
Python :: get the number of today week python 
Python :: min max and avg function of python 
Python :: hello worldpython 
Python :: python get minute from datetime 
Python :: button images in tkinter 
Python :: create pyspark session with hive support 
Python :: python get list of files in path 
Python :: how to rotate the x label for subplot 
Python :: python map input 
Python :: django auto increment field 
Python :: how to dynamically access class properties in python 
Python :: sigmoid function numpy 
Python :: convert int to byte python 
Python :: Sin , Cos Graph using python turtle. 
Python :: how to print whole year calendar in python 
Python :: how to display qr code in python 
ADD CONTENT
Topic
Content
Source link
Name
2+3 =