This is a Python Program to find the sum of digits in a number.
The program takes in a number and finds the sum of digits in a number.
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.
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)
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.
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.
- Check Python Books
- Apply for Python Internship
- Check Information Technology Books
- Practice Programming MCQs
- Apply for Programming Internship