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.
Discover more from Python Mania
Subscribe to get the latest posts sent to your email.