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
- Get the input from user.
- 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