Sum of The Digits 2 :- Given an integer between 0 and 10000, write a program to print the sum of its digits.

PHOTO EMBED

Fri Nov 04 2022 18:54:15 GMT+0000 (Coordinated Universal Time)

Saved by @codewithakash

number = input()

is_one_digit = (len(number) == 1)
is_two_digit = (len(number) == 2)
is_three_digit = (len(number) == 3)

if is_one_digit:
  print(number)
elif is_two_digit:
  first_digit = int(number[0])
  second_digit = int(number[1])
  print(first_digit + second_digit)
elif is_three_digit:
  first_digit = int(number[0])
  second_digit = int(number[1])
  third_digit = int(number[2])
  print(first_digit + second_digit + third_digit)
else:
  first_digit = int(number[0])
  second_digit = int(number[1])
  third_digit = int(number[2])
  fourth_digit = int(number[3])
  print(first_digit + second_digit + third_digit + fourth_digit)
content_copyCOPY

Explanation For example, if the given number is 25, your code should print the sum of the digits (2 +5), which is 7. Similarly, if the given number is 692, your code should print the sum of the digits (6 +9+ 2), which is 17. Similarly, if the given number is 9999, your code should print the sum of the digits (9+9+9+ 9), which is 36.

https://learning.ccbp.in/crash-course/coding/efd285cb-2566-4aea-b406-de162be87464