Write a Python program to print the square of first 10 natural numbers, using while loop
Question
Write a Python program to print the square of first 10 natural numbers, using while loop
Solution
Sure, here is a simple Python program that prints the square of the first 10 natural numbers using a while loop:
# Initialize the counter
counter = 1
# Use a while loop to iterate over the first 10 natural numbers
while counter <= 10:
# Calculate the square of the current number
square = counter ** 2
# Print the square
print(f'The square of {counter} is {square}')
# Increment the counter
counter += 1
This program starts with a counter set to 1. It then enters a while loop that continues until the counter is greater than 10. Inside the loop, it calculates the square of the current counter value, prints it, and then increments the counter.
Similar Questions
What will be the output of the following program?while i < 20: print(i) i += 1
What does the following code print?import mathx=100while(math.sqrt(x)>0): print("Square root larger than 0")x -= 5
What will be the output for the following code?i=0while(i<3): j=0 while(j<3): print(i,j,end=" ") j+=1 print()
Please answer the following questions to assess your level: How many times is the following loop executed?1234 i=100 while(i<=200): print(i) i+=20
What is the last number printed when the following code is run?number = 0while number <= 10:print("Number: ", number)number = number + 1100111
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.