0
#how can correct this code #i m trying to get the addition of all digits of entered number
#how can correct this code #i m trying to get the addition of all digits of entered number n = int(input()) length = 0 while (n)>0: length =length+(n % 10) n=n/10 print(int(length))
7 Answers
+ 4
Here are 2 versions that can do this task:
# when input is string:
inp = '1234'
print(sum([int(i) for i in inp]))
# when it should be done with integers
inp = 1234
new = []
while inp > 0:
inp, mod = divmod(inp,10)
new.insert(0,mod)
print(sum(new))
+ 3
Abhishek
What you want to do is not very clear. Addition of the digits or the length?
Anyways, check this out, it might help.
https://code.sololearn.com/cTgx4gYfpto8/?ref=app
+ 1
Sum of the digits or count of digits?
+ 1
n = input()
print(len(n))
+ 1
Abhishek
Above code works fine đđ
0
I am trying to find out the addition of digits which is entered by user input
Need to use while loop only
Above code is giving almost correct output just getting addition of 1,
If input 1234
Output should be 10
0
Chk with 6 0r 7 digit entry