Computer Science, asked by ssbhandari0069, 2 months ago

Q.24: To input a number and print its sum of digit.python ​

Answers

Answered by anindyaadhikari13
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:

  1. n (integer):- For storing the entered number.
  2. s (integer):- For storing the sum of the digits of n.

Output is attached.

Attachments:
Answered by Anonymous
5

Answer:

\huge\rm{\fcolorbox{green}{aqua}{Program:–}}

\rm \: n=\purple{int}{(\purple{input}}(\green{"Enter  \: a \: number"}))

\rm \: tot = 0

\rm\orange{while}(n>0): \\ \rm \: dig=n÷10 \\ \rm \: tot=tot+dig \\ \rm\: n=n//10

\rm\purple{print}({\green{ \: "the  \: to \: sum \: of \: the \: digits \: is:"}}),tot)

After you Complete doing this program click on "Run the program"

Similar questions