A
A
Andrey Nill2020-05-22 18:52:58
Python
Andrey Nill, 2020-05-22 18:52:58

Find the sum of the digits of a four-digit number, help?

Given a four-digit number. Find the sum of its digits.
Examples
Input
Output
2020
4
An example of finding a three-digit number:
x = int(input())
x1 = x//100
x2 = (x//10)%10
x3 = x%10

Do the same for this example, operators at a higher site level does not accept

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Anton Muravyov, 2020-05-22
@AndreyNill

print(sum(map(int, list(input()))))
For any number of digits in a number

N
Nazar Tropanets, 2020-05-22
@nazartropanets

number = str(input('Введите число: '))
sum_ = 0
for char in number:
    sum_+= int(char)
print(sum_)

works with any numbers, even thousands of digits

S
szafranji, 2020-05-22
@szafranji

Convert this number to a string and count its individual elements.

x = input()
summa = 0
for i in x:
    summa += int(i)
print(summa)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question