You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
17 lines
425 B
Python
17 lines
425 B
Python
5 years ago
|
# Python program to find the sum of natural numbers up to n where n is provided by user
|
||
|
|
||
|
# change this value for a different result
|
||
|
num = 16
|
||
|
|
||
|
# uncomment to take input from the user
|
||
|
#num = int(input("Enter a number: "))
|
||
|
|
||
|
if num < 0:
|
||
|
print("Enter a positive number")
|
||
|
else:
|
||
|
sum = 0
|
||
|
# use while loop to iterate un till zero
|
||
|
while(num > 0):
|
||
|
sum += num
|
||
|
num -= 1
|
||
|
print("The sum is",sum)
|