-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforrangesofNnum.py
More file actions
38 lines (30 loc) · 796 Bytes
/
forrangesofNnum.py
File metadata and controls
38 lines (30 loc) · 796 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# print sum of first n numbers
n=int(input('enter a number:')) # n = 5
sum=0
for num in range(1,n+1):
sum+=num
print(sum)
'''
Step 1: Initialize variables
User inputs n=5
Initialize sum = 0 to accumulate the total
Step 2: The for loop begins iterating over the range from 1 to n+1
i.e., 1 to 6 (exclusive of 6)
Step 3: First iteration (num = 1)
Add num (1) to sum
sum = 0 + 1 = 1
Step 4: Second iteration (num = 2)
Add num (2) to sum
sum = 1 + 2 = 3
Step 5: Third iteration (num = 3)
Add num (3) to sum
sum = 3 + 3 = 6
Step 6: Fourth iteration (num = 4)
Add num (4) to sum
sum = 6 + 4 = 10
Step 7: Fifth iteration (num = 5)
Add num (5) to sum
sum = 10 + 5 = 15
Step 8: The loop ends as all values from 1 to 5 have been processed
Step 9: Print the final value of sum, which is 15
'''