Q.24: To input a number and print its sum of digit.python
Answers
Answered by
3
Required Answer:-
Question:
- Write a program in Python to print it's sum of digits.
Program:
n=int(input("Enter an integer: "))
s=0
while n!=0:
s+=n%10
n=n//10
print("Sum of digits: ",s)
Result:
Enter an integer: 2020
Sum of digits: 4
[Program Finished]
How its solved?
First of all, we will input an integer number from the user. Then, we will create a while loop that will iterate until the number is not zero. Then, we will extract each digits of the number and add to the variable s. So, sum of the digits is now stored in s. Time to print.
Variable Description:
- n (integer):- For storing the entered number.
- s (integer):- For storing the sum of the digits of n.
Output is attached.
Attachments:
Answered by
5
Answer:
After you Complete doing this program click on "Run the program"
Similar questions
Math,
2 months ago
Social Sciences,
2 months ago
Math,
2 months ago
Math,
4 months ago
Social Sciences,
4 months ago
Math,
10 months ago
Math,
10 months ago
Geography,
10 months ago