Robotics & Artificial Intelligence
Write a program to enter a number and display the sum of its digits.
Sample Input: 642
Output: 12 (6+4+2 = 12)
Python Control Flow
3 Likes
Answer
n = int(input("Enter a number: "))
s = 0
while(n != 0):
d = n % 10
s = s + d
n = n // 10
print("Sum of digits =", s)Output
Enter a number: 642
Sum of digits = 12
Answered By
3 Likes
Related Questions
Write a program to find and display the sum of any ten natural numbers.
Write a program to input any 20 numbers (including positive and negative). The program displays the sum of all positive numbers and negative numbers separately.
Write a Python code to check and display whether a number input by the user is a composite number or not.
[A number is said to composite, if it has one or more factors excluding 1 and the number itself. For example: 4, 6, 8, 9, ……………]
Write a program to input 10 different numbers. Display the greatest and the smallest numbers among them.