+ 10

How to print sum of all numbers in the any digit number in python?

How to print sum of all numbers in any digit number of user input in python like 👇 123 = 1+ 2+ 3 =6 67890 6+7+8+9+0 =30

3rd Feb 2018, 5:28 AM
Ratnapal Shende
Ratnapal Shende - avatar
6 Answers
+ 9
a=int(input()) print(sum([int(x) for x in list(str(a))]))
3rd Feb 2018, 6:30 AM
Louis
Louis - avatar
+ 6
n=123; sum=0; while (n>0) { d=n%10; //for make the digit single sum=sum+d; // for adding the digit which we get from d variable. n=n/10; //to keep on making dividing the digit. }
3rd Feb 2018, 6:15 AM
🩋FEATHER🩋
🩋FEATHER🩋 - avatar
+ 5
Input from user
3rd Feb 2018, 11:02 AM
Ratnapal Shende
Ratnapal Shende - avatar
+ 2
#how to sum all the digits of a given number num=str(input('Choose a number:')) i=len(num) sum=int(num[i-1]) while i>1: i-=1 sum+=int(num[i-1]) print ("the sum of all the digits in",str(num),"is",str(sum))
24th Jan 2021, 9:56 AM
José Javier Castrillo Plaza
José Javier Castrillo Plaza - avatar
+ 1
How to do that in JavaScript?
16th Nov 2018, 12:00 PM
Benjamin Carreon
Benjamin Carreon - avatar
0
n=input('enter the digits you want: ') p=n.split() q=0 for i in n: q=int(q)+int(i) print('sum of digits is: ',q)
22nd Dec 2021, 11:14 AM
SANVIKA