View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
Given a number and the task is to find sum of digits of this number in Python.
Examples:
Input : n = 87
Output : 15
Input : n = 111
Output : 3
Below are the methods to sum of the digits.
Method-1: Using str[] and int[] methods.: The str[] method is used to convert the number to string. The int[] method is used to convert the string digit to an integer.
Convert the number to string and iterate over each digit in the string and after converting each digit to integer and add to the sum of the digits in each iteration.
Python3
def
getSum[n]:
sum
=
0
for
digit
in
str
[n]:
sum
+
=
int
[digit]
return
sum
n
=
12345
print
[getSum[n]]
Output:
15
Method-2: Using sum[] methods.: The sum[] method is used to sum of numbers in the list.
Convert the number to string using str[] and strip the string and convert to list of number using strip[] and map[] method resp. Then find the sum using the sum[] method.
Python3
def
getSum[n]:
strr
=
str
[n]
list_of_number
=
list
[
map
[
int
, strr.strip[]]]
return
sum
[list_of_number]
n
=
12345
print
[getSum[n]]
Output:
15
Method-3: Using General Approach:
- Get the number
- Declare a variable to store the sum and set it to 0
- Repeat the next two steps till the number is not 0
- Get the rightmost digit of the number with help of remainder ‘%’ operator by dividing it with 10 and add it to sum.
- Divide the number by 10 with help of ‘//’ operator
- Print or return the sum
A. Iterative Approach:
Python3
def
getSum[n]:
sum
=
0
while
[n !
=
0
]:
sum
=
sum
+
[n
%
10
]
n
=
n
/
/
10
return
sum
n
=
12345
print
[getSum[n]]
Output:
15
B. Recursive Approach:
Python3
def
sumDigits[no]:
return
0
if
no
=
=
0
else
int
[no
%
10
]
+
sumDigits[
int
[no
/
10
]]
n
=
12345
print
[sumDigits[n]]
Output:
15
This is a Python Program to find the sum of digits in a number. Problem Description
The program takes in a number and finds the sum of digits in a number.
Problem Solution
1. Take the value of the integer and store in a variable.
2. Using a while loop, get each digit of the number and add the digits to a variable.
3. Print the sum
of the digits of the number.
4. Exit.
Program/Source Code
Here is the source code of the Python Program to find the sum of digits in a number. The program output is also shown below.
n=int[input["Enter a number:"]] tot=0 while[n>0]: dig=n%10 tot=tot+dig n=n//10 print["The total sum of digits is:",tot]
Program Explanation
1. User must first enter the value and store it in a variable.
2. The while loop is used and the last digit of the number is obtained by using the modulus operator.
3. The digit is added to another variable each time the loop is executed.
4. This loop terminates when the value of the number is 0.
5. The total sum of the number is then printed.
Runtime Test Cases
Case 1: Enter a number:1892 The total sum of digits is: 20 Case 2: Enter a number:157 The total sum of digits is: 13
Sanfoundry Global Education & Learning Series – Python Programs.
To practice all Python programs, here is complete set of 150+ Python Problems and Solutions.
Next Steps:
- Get Free Certificate of Merit in Python Programming
- Participate in Python Programming Certification Contest
- Become a Top Ranker in Python Programming
- Take Python Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.
Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.