Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

while loop in python

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

n = 10
sum = 0
i = 1

while i <= n:
    sum = sum + i
    i = i+1    # update counter

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

# To take input from the user,
# n = int(input("Enter n: "))
Source by www.programiz.com #
 
PREVIOUS NEXT
Tagged: #loop #python
ADD COMMENT
Topic
Name
1+1 =