In this blog we will see how to add the digits splitter using % and //operator.
no = 1234
tot = 0
while no>0:
rem = no % 100
print(rem)
tot = tot + rem
no = no // 100
print(tot)
print("-----------------")
gokulmac@Gokuls-MacBook-Air DS - Python % /usr/local/bin/python3 "/Users/gokulmac/Desktop/DS -
Python /DS3.py"
34
12
46
-----------------
no = 1234
tot = 0
while no>0:
rem = no % 100
print(rem)
tot = tot + rem
no = no // 10
print(tot)
print("-----------------")
34
23
12
1
70
-----------------
no = 1234
tot = 0
while no >= 10:
rem = no % 100
print(rem)
tot = tot + rem
no = no // 10
print(tot)
print("-----------------")
34
23
12
69
-----------------
no = 123456
while no > 0:
rem = no % 1000
print(rem)
no = no // 1000
print("-----------------")
456
123
-----------------
gokulmac@Gokuls-MacBook-Air DS - Python %
Top comments (0)