Python 3 Program to Find the Sum of Digits in a Number Tutorial For Beginners
1K views
Jun 1, 2025
Get the full source code of application here:
View Video Transcript
0:00
uh hello guys welcome to this video so
0:02
in this video I will show you a Python
0:04
program which will allow you to
0:06
calculate the sum of all digits present
0:09
in a number so this program is really
0:12
interesting it is asked inside
0:14
interviews and competitive programming
0:16
as well so it basically uh whenever let
0:19
me just execute this program and tell
0:21
you how it executes so inside my VS code
0:24
let me just execute this so here it will
0:27
ask you to enter a number so I just need
0:29
to enter a number let's suppose I write
0:32
145 so 145 what this program will do it
0:36
will actually calculate the sum of each
0:39
digit so 1 1 +
0:43
4 +
0:46
5 so the answer the output should be 10
0:50
5 + 4 + 1 so which comes out to be 10 so
0:53
if I execute this now you will see sum
0:56
of digits is equal to 10 so very simple
1:00
program we take each digit which is
1:03
present and then we calculate the sum of
1:05
it let me take another example if I say
1:09
3 4 5 6 fourdigit number now what
1:13
happens it calculates 3 + 4 + 5 + 6 so
1:21
now it comes out to be 7 + 5 12 + 6 18
1:26
so the output should be 18 so now you
1:29
will see sum of digits is 18 so now let
1:32
me show you the actual program right
1:35
here so we actually ask using the input
1:38
function enter a number then we declare
1:40
a variable sum of digits is equal to
1:42
zero and for this we are using while
1:45
loop we are looping through all the
1:49
actual number here so while the number
1:51
is greater than
1:52
zero so here what we are doing we are
1:55
getting the last digit by using the
1:58
percentage operator so let me paste this
2:03
program and show this visual execution
2:06
of this program so for this I will use
2:10
this famous website which is
2:14
pythonutor.com it's a very good website
2:17
if you want to visualize programs step
2:19
by
2:20
step so this is the website here let me
2:24
paste this program and try to execute
2:27
visual execution step by step so here it
2:30
will actually ask you to enter a
2:35
digit so it is asking you to enter a
2:38
number so let me write a number here 3 4
2:40
5 6 and click on submit so as soon as
2:44
you click submit the number is
2:46
registered as you can see as a global
2:48
variable now we come to the next step we
2:50
declare a variable sum of digits and
2:53
this variable is set to zero so this
2:55
variable is registered here this is set
2:57
to zero now we come to the while loop
2:59
here you can see while number is greater
3:02
than zero as you can see our number is 3
3:04
4 5 6 it is greater than zero so this
3:08
condition evaluates to true now we will
3:10
come to this while loop the very first
3:12
thing we are doing here we are
3:14
extracting the last digit from this
3:16
number so what is the last digit six as
3:20
you can see the last digit is six we
3:22
using this operator which is percentage
3:24
operator so what it is doing it is
3:27
dividing it by 10 and getting the
3:29
remainder so what comes when you divide
3:32
3 4 5 6 by
3:35
10 it basically we get the last digit as
3:40
a remainder
3:41
so 10 divided by 3 4 5 6 you just
3:45
calculate by itself just divide
3:49
this 10 3 are
3:53
30 we get
3:56
45 and then 10 4 are
4:00
40 5 6 and 10 5
4:05
are 50 so we get the six as the
4:08
remainder so exactly we are doing the
4:10
last extracting the last digit so we are
4:13
getting six here and now in the next
4:15
step we are adding this digit what we
4:19
got six right here we are adding it to
4:23
this variable which is by default zero
4:26
so now in the next step we add this
4:29
to this
4:31
variable now again we divide this number
4:35
that we got here
4:38
this number by 10 so this is not the
4:42
this operator this is called as
4:44
percentage this is used to get the
4:46
remainder so now this is a uh division
4:49
operator so now if you divide this by 10
4:53
we get 3 4 5 so as you can see this
4:56
digit have been
4:58
removed so in the previous step we just
5:02
want to remove
5:03
this so that's why we divided this by 10
5:07
as soon as we do this we remove the last
5:10
digit now we need to
5:13
get this five so what we do we repeat
5:17
the process so 3 4 5 is greater than
5:20
zero now again we divide this by 10 to
5:24
get
5:25
this five here the last digit so again
5:29
we get the last digit in this variable
5:32
as you can see previously it was six so
5:35
now again we divide we get the remainder
5:38
and it is stored inside
5:40
digit you can see when you observe this
5:43
it's a very simple program what we are
5:46
doing we are getting the last digit
5:48
every
5:50
time and then we add this to the sum of
5:53
digits so 6 + 5 it will be 11 so as you
5:58
can see 6 + 5 11 again we divide this
6:01
and we remove this five for removing
6:04
this we divide this whole number by 10
6:07
so now this now this time this becomes
6:09
34 again we do the uh while loop again
6:14
we compare 34 is greater than zero now
6:17
once again we need to get this digit
6:19
which is
6:21
four
6:22
we apply this
6:24
percentage now this becomes four 4 + 11
6:30
now it becomes
6:32
15 again we divide this to remove this
6:36
four now this this becomes three again
6:40
we compare it three is greater than
6:43
zero again we get the remainder which is
6:47
three so digit now will be three so 3 +
6:52
15 is comes out to be 18 so this will be
6:55
your output 18 so if I say now this
6:59
becomes 18 previous this was 15 15 + 3
7:03
is now
7:04
18 so after that now we will compare
7:09
that if
7:12
three we will divide this 3 by 10 so 3x
7:17
10 now number becomes zero so now 0 is
7:22
not greater than zero so now the while
7:23
loop will break and now it will print
7:26
out sum of
7:27
digits in the next step is
7:31
18 so very simple program guys you can
7:34
see how simple it is inside the while
7:36
loop first of all we are removing the
7:39
last digit to get the last digit we are
7:42
using the percentage sign then we are
7:44
adding it to this sum of digits and then
7:46
we are dividing it to remove the last
7:49
digits by the division operator it's a
7:51
very simple program it will be asked
7:53
inside interviews and competitive exams
7:56
you can explain it like this and thank
7:58
you very much for watching this video
8:00
and also check out my website
8:02
freeattools.com
8:05
uh which contains thousands of tools