Write a Python Program to Find the Sum of n Natural Positive Numbers Tutorial For Beginners
570 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 how to
0:04
write a Python program which calculates
0:06
the sum of uh uh natural numbers you can
0:11
actually give it a range here I want the
0:13
sum of all the natural numbers up to
0:15
this range and this then this Python
0:17
program will calculate the sum and
0:19
return to you this is a very common
0:21
question asked inside interviews and
0:23
competitive exams as well so let me show
0:25
you the output first of
0:27
all as I execute this program right here
0:31
it will ask you to enter a positive
0:33
number up to which so now let's suppose
0:36
I want all the sum of all positive
0:40
natural numbers up to 10 so now you can
0:43
see sum of natural numbers up to 10 is
0:46
55 so how it calculates it calculates
0:49
like this 1 + 2 +
0:54
3
0:56
+ 4 + 5 + 6 + 7 very simple
1:04
program how many numbers you write it
1:08
basically manually calculates the sum so
1:11
1 2 3 6 10 15 21 28 28 +
1:18
8 36 36 + 10 46 46 + 9 uh 46 55 so this
1:28
is your sum here 55 if I say let's
1:31
suppose I want all the sum of positive
1:35
numbers up to three so this will be 1 +
1:37
2 + 3 which is 6 so you will see so let
1:42
me now show the actual program right
1:44
here it's very simple let me show you
1:47
step by step using this famous tool
1:51
which is Python tutor which will execute
1:54
program step by step so here the very
1:56
first step we are asking the user to
1:58
enter a positive number so we let me
2:00
write five so I want the sum of all the
2:05
positive numbers up till five as I
2:07
submit you will
2:09
see it will register a global
2:13
variable number will be initialized to
2:16
five and then for this program guys we
2:19
need an additional variable to store the
2:21
sum of numbers so we are declaring this
2:24
global variable sum is equal to zero at
2:27
the starting so we have this variable
2:30
sum is declared to zero now we are
2:32
having this for loop here we are looping
2:35
through all the numbers that the user
2:38
has written up till five so now the loop
2:41
will start from 1 up till
2:45
5 so that's why we are using this for
2:48
loop for i in range 1 comma number is 5
2:54
+ 1 which is 6 so 6 is not included it
2:59
will run until five and after it reaches
3:02
six it will break so essentially it is
3:05
running from 1 to 5 and inside this for
3:08
loop we are
3:10
simply just storing the sum here so sum
3:13
whatever is the sum is equal to sum
3:17
plus what is the number here so 1 then 2
3:21
3 4 5 so the first step I is equal to 1
3:26
here you will see I is 1 so now the sum
3:29
will be 1 + 0 which will comes out to be
3:32
1 now sum will become 1 now it will run
3:37
for the second time now the i value will
3:39
become two so 2 +
3:43
1 which will be three so now this will
3:47
be three so now again i value will
3:49
increase to three so 3 + 3 is
3:54
6 so now I will become four
3:58
so 4 + 6 is 10 as you can see and then I
4:04
will become 5 so 5 + 10 comes out to be
4:07
15 and as soon as I reaches six the loop
4:12
will break and then it will print out
4:15
sum of natural numbers up to five is 15
4:19
so this is a very simple approach by
4:22
which you can solve this problem in
4:24
Python very simple program as you can
4:27
see
4:28
please hit that like button subscribe
4:30
the channel guys and also check out my
4:32
website freemediattools.com
4:35
uh which contains thousands of tools
#Programming
#Scripting Languages