Basic Programs

If Else Programs

While loop Programs

For loop Programs

List Programs


Python Program to Find the Sum of Natural Numbers Using While Loop


In this example shows, how to find the sum of the first n natural numbers using a while loop.

Steps
  1. Get the input from user.
  2. The program calculates the sum of the first N natural numbers using a while loop
Here the example, sum of n natural numbers using while loop.
Example.py
n = int(input("Enter a Number: "))
i = 1
sum = 0
while (i <= n):
    sum = sum + i
    i = i + 1
print("The sum is: ", sum)

Output 1

Enter a Number  : 5
The sum is :  15

Prev Next