Sum of the digit of number in python by R4R Team

Sum of digit means that we add the digit of the number like
a=1225
then it sum of digit is 1+2+2+5 which is 10

program -

print("Enter any number")
n=int(input())
ans=0
while(n):
t=n%10
ans=ans+t
n=int(n/10)

print("Sum of digit is- ",end=" ")
print(ans)


output -

Enter any number
1224
Sum of digit is- 9


- In this program, firstly we take input of number and run while loop in number of digits times and % operator 10 is pick the each digit and add in 'ans' and at last we print the 'ans' which is the sum of the digit of the given number.
Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!