Skip to content

Commit 3b9188e

Browse files
Merge pull request #1963 from sarayusreeyadavpadala/add
Comments are added
2 parents 6e026e5 + 74ff654 commit 3b9188e

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

Sum of digits of a number.py

+12-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1-
q=0
2-
n=int(input("Enter Number: "))
3-
while(n>0):
4-
r=n%10
5-
q=q+r
6-
n=n//10
1+
q=0 # Initially we assigned 0 to "q", to use this variable for the summation purpose below.
2+
# The "q" value should be declared before using it(mandatory). And this value can be changed later.
3+
4+
n=int(input("Enter Number: ")) # asking user for input
5+
while n>0: # Until "n" is greater than 0, execute the loop. This means that until all the digits of "n" got extracted.
6+
7+
r=n%10 # Here, we are extracting each digit from "n" starting from one's place to ten's and hundred's... so on.
8+
9+
q=q+r # Each extracted number is being added to "q".
10+
11+
n=n//10 # "n" value is being changed in every iteration. Dividing with 10 gives exact digits in that number, reducing one digit in every iteration from one's place.
12+
713
print("Sum of digits is: "+str(q))

0 commit comments

Comments
 (0)