Skip to content

Commit 1ce9b3e

Browse files
Create To find sum of natural numbers
To test the program for a different number, change the value of num. Initially, the sum is initialized to 0. And, the number is stored in variable num.
1 parent 99cf7d7 commit 1ce9b3e

1 file changed

Lines changed: 13 additions & 0 deletions

File tree

To find sum of natural numbers

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Sum of natural numbers up to num
2+
3+
num = 16
4+
5+
if num < 0:
6+
print("Enter a positive number")
7+
else:
8+
sum = 0
9+
# use while loop to iterate until zero
10+
while(num > 0):
11+
sum += num
12+
num -= 1
13+
print("The sum is", sum)

0 commit comments

Comments
 (0)