Python Program to Find the Sum of Natural Numbers

Created with Sketch.

Python Program to Find the Sum of Natural Numbers

Natural numbers:

As the name specifies, a natural number is the number that occurs commonly and obviously in the nature. It is a whole, non-negative number.

Some mathematicians think that a natural number must contain 0 and some don’t believe this theory. So, a list of natural number can be defined as:

 

N= {01234, …. and so on}

N= {1234, …. and so on}

See this example:

  1. num = int(input(“Enter a number: “))
  2. if num < 0:
  3.    print(“Enter a positive number”)
  4. else:
  5.    sum = 0
  6.    # use while loop to iterate un till zero
  7.    while(num > 0):
  8.        sum += num
  9.        num -= 1
  10.    print(“The sum is”,sum)

This example shows the sum of the first 100 positive numbers (0-100)

Output:

Python Condition And Loops12

 

Leave a Reply

Your email address will not be published. Required fields are marked *