Calculate Product of Numbers from 1 to 10 Using While Loop in Python

In this post, we are going to discuss how to calculate the product of numbers from 1 to 10 using a while loop in Python:

Simple code:

num = 1
product = 1
while num <= 10:
    product *= num
    num += 1
print(product)

Output:

3628800


Code with comments:

#initialize the starting number and product to 1
num = 1
product = 1
#continue looping until the number reaches 10
while num <= 10:
    # multiply the current number to the product
    product *= num
    # increment the number by 1
    num += 1
# print the final product
print(product)

Output:

3628800

In the above code, we initialize the starting number and product to 1.

We then use a while loop to continue multiplying numbers from 1 to 10 to the product variable.

Than we increment the number by 1 at each iteration of the loop and continue until we reach 10.

Finally, we print the product value, which is the product of all the numbers between 1 and 10.

Was this helpful?
YesNo

Related Articles:

Recent Articles:

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x