Python while loop

PHOTO EMBED

Tue Mar 31 2020 11:54:39 GMT+0000 (Coordinated Universal Time)

Saved by @amnawidow #python #pyhton #loops #whileloop

# Program to add natural
# numbers upto 
# sum = 1+2+3+...+n

# To take input from the user,
# n = int(input("Enter n: "))

1.n = 10

# initialize sum and counter
2.sum = 0
3.i = 1

4.while i <= n:
   5. sum = sum + i
   6. i = i+1    # update counter

# print the sum
7.print("The sum is", sum)

When you run this code output will be:
Enter n: 10
The sum is 55
content_copyCOPY

The above code is how to use while loop to print sum of any number.

ttps://www.programiz.com/python-programming/while-loop