Sum of digits of a number in Python

Sum of digits of a number in Python

In this article, we are going to understand and perform a simple python program, in which, we have a number, and we are required to calculate the sum of all the digits in the given number. For example, we are given the number 1234, so the sum of all the digits in the given number should be 1 + 2 + 3 + 4, which is 10.

As a prerequisite for understanding this program, you need to have knowledge about some concepts like –

while loop
if else statements.

Program Approach

Lets us first make sure that we have understood what are we supposed to do. Here, we have a number, and we are supposed to calculate the sum of all the digits in the given number.

For this, we are going to have a simple approach, where we are going to take one digit from the given number, and update the sum by adding that digit to the sum. We continue to do this, till our number is not zero. Let’s see how are we going to do this.

  • Let’s say that the number is initially 1234.
  • We have a variable sumofdigits, which is going to have the sum of all the digits from the number. Initially, we set it to 0.
  • We get the remainder while dividing the number by 10, through which, we get the last digit of the number. In our case, the last digit we get would be 4 initially, and then we add it to the sumofdigits. So, the new value with sumofdigits comes out to be 4.
  • As the next step, we are done with the last digit, so we are going to remove it from the number, by dividing the number by 10.
  • So now, the number becomes 123.
  • We repeat the above steps again, till the number becomes 0. Once the number is 0, we would move out of the loop.

If you are confused reading the above steps, do not worry, since we are going to have a look at the program now, which would clear most of the things.

Sum of Digits Program in Python

————————————-Program starts here———————————

number = int(input(“Please enter the number:”))
sumofdigits = 0
while number!=0:
    remainder = number % 10
    sumofdigits +=remainder
    number = number // 10

print(“Sum of all the digits in the number:”, sumofdigits)

 

——————————–Program ends here—————————————-

 

As you can see in the above program, we are getting the number as input from the user. After that, we have the sumofdigits variable, which is going to hold the sum of all the digits from the number.

After that, while the number is not zero, we are going to execute some blocks of code repeatedly. After we are out of the loop, we have the sum of digits.

Let’s have a look at the output now –

————————————-Output here——————————————-

Please enter the number:1234
Sum of all the digits in the number: 10

————————————-Output ends——————————————

As you can see from the output, we enter the number, and we get the sum of all the digits in the number.

Let’s see how the program has executed step by step, with the values –

Firstly, the number we have is 1234.
Initially, the sumofdigits is 0.

First iteration –

  • remainder = 1234 % 10 → 4
  • sumofdigits = 0 + 4 → 4
  • number = 1234//10 → 123

In the next iteration, the number is 123, and the sumofdigits is updated to 4.
Second iteration –

  • remainder = 123 % 10 → 3
  • sumofdigits = 4 + 3 → 7
  • number = 123//10 → 12

In the next iteration, the number is now 12, and the sumofdigits is updated to 7.
Third iteration –

  • remainder = 12 % 10 → 2
  • sumofdigits = 7 + 2 → 9
  • number = 12//10 → 1

In the next iteration, the number is now 1, and the sumofdigits is updated to 9.
Fourth iteration –

  • remainder = 1 % 10 → 1
  • sumofdigits = 9 + 1 → 10
  • number = 1//10 → 0

As you can see in this iteration, the number has become 0, due to which, we are not going to enter the loop again, and we have our sum of digits stored into the variable sumofdigits, which comes out to be 10.

So, this way, you can try the program for some other inputs as well, and you can try to visualize like we did above so that we can understand the concept better.

Conclusion:

In the above program, we calculated the Sum of the digits of a number in Python. For example, if the number is 1234, the sum of all the digits comes out to be 1 + 2 + 3 + 4, which is 10.

FAQ About Sum of digits of a number in Python

Q: How to get the last digit of some numbers?

Ans: To get the last digit of some number, we just need to get the remainder when we divide the number by 10.

Q: How to find the sum of all the digits in the number?

Ans: To find the sum of all the digits in the number, we just need to get the remainder and add it to the variable holding the sum, and then we need to divide the number by 10 so that the digit which we are done adding is not included. We continue to do this, till the number becomes zero, and once the number becomes zero, we have our sum of all the digits from the number.