Python Program Print The Sum Of Digits - Codicaly


In this post, we are going to write a python program to print the sum of digits of a number. 





General Idea :
To find the sum of the digits of a number, first, we are going to put the digits of that number in an empty list, then we are going to use the sum function which basically prints the sum of the elements present in it.

Pre - Requisites :
  • Python Basics
  • Iteration ( For loop )
  • List
Program :

num=input("Enter an integer")
l=len(num)
n=int(num)
list1=[]
for i in range(0,l):
    d=n%10
    n=int(n/10)
    list1.append(d)
print("The Sum of Digits of ",num," is ",sum(list1))













Explanation :



1.First, we need to input the integer for which we are finding the sum of digits. Here we are taking the input as a string as we need to find its length and we are assigning it to num.




2. As we took the input as a string we can find its length. This is nothing but the number of digits number. If we enter 2343 as input, its length will be 4, as there are 4 digits. We are assigning that to l. ( Note : Find the length is only applicable for string, list, and  not for integer.)




3.We need to convert that into an integer for subsequent calculations. Hence we are converting the string into an integer using the int function and assigning that to n.




4. We are creating an empty list here. In this list only we are going to fill in the digits of the inputted number. From this list only we can find the sum using the sum function.




5.We are creating a for loop for subsequent calculations. The start value is 0 and the end value is l. That means the calculations under this loop will repeat l times. If a 4-Digit number, the calculations will be repeated 4 times.




6. This statement is written under the for loop created in step 5. The operator used in this step is % (Modulus Operator). This will give the remainder when n is divided by 10. In general, when a number is divided by 10, its remainder will be its last digit ( unit digit ). If 12 is divided by 10, its remainder will be 2 which is its unit digit.




7. This statement is also written under the for loop created in step5. The operator used in this step is / (Division). By doing this step a four-digit number will be converted to a three-digit number. If we divide 4537 by 10, it will be 435.7. This is a float ( decimal ). We specified int in the statement. Hence whatever digit after the decimal point will be removed. Hence n will be 435 in this case. Hence converted a four-digit number into a three-digit number.




8. This statement is written under the for loop created in step 5. This will add the last digit (d) to the empty list created in step 3.

This loop will repeat till the empty list gets all the digits of the inputted number. If we enter 123, its length is 3. That means the loop will be repeated 3 times.
  •  First, d will be 3 as it's the last digit. Then n will be 12 after executing step 7. Then 3 will be added to the empty list.
  • Second, d will be 2 as when n=12 divided by 10 leaves 2 as remainder. The n will be 1 after executing step 7. Then 2 will be added to the list.
  • Third, d will be 1 as 1 divided by 10 leaves 1 as remainder. Then n will be 0 after executing step 7. Then 1 will be added to the list.
Now we can see there are 3 steps as the entered number is a 3-digit number. 




9. This statement is written separately apart from the for loop created. We need to print the sum of the digits. So we are using the sum function which basically prints the sum of integers present in the list. We are using the print function to print.This will print [ The Sum of Digits of ( inputted number ) is ( sum of digits) ].

Input/Output :

3-Digit Number


2-Digit Number

1-Digit Number



Thank you












Comments

Popular posts from this blog

Python Program to Print Fibonacci series - Codicaly

Python - Program to find the given number is a prime or composite number

Python Program To Replace The Elements of List With its Cube