Sum of digits in a Number - Learn Engineering

All the news and updates relevant to Engineering. You can also Write a post here

Breaking

Friday, June 19, 2020

Sum of digits in a Number

python-mini

Question

To Count the sum of digits in a number.

Sample TestCaseExpected Output

Enter a number : 1234                                                                        Total sum of digits is 10




Answer


n=int(input("Enter a number : "))
tot=0
while n>0:
    dig=n%10
    tot+=dig
    n//=10
print("Total sum of digits is ",tot)   

No comments:

Post a Comment