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
python code to square every num in the list
Write a Python program to generate a series of 10 natural numbers.
Square of a NumberYou are given a Python code snippet that aims to demonstrate list comprehension to create a new list of squared numbers from a given list of integers. However, some parts of the code are missing. Fill in the missing code to complete the program.Sample Test CasesTest Case 1:Expected Output:Enter·a·list·of·integers·separated·by·commas:·1,2,3,4New·list·of·squared·numbers:·[1,·4,·9,·16]Submit1234567891011121314#·Fill·in·the·missing·code·below·to·demonstrate·list·comprehension¬¬#·Prompt·the·user·to·enter·a·list·of·integers·separated·by·commas¬numbers·=·input("Enter·a·list·of·integers·separated·by·commas:·").split(',')¬¬#·Convert·the·input·strings·to·integers¬numbers·=·[int(num)·for·num·in·numbers]¬¬#·Fill·in·the·missing·code·to·create·a·new·list·of·squared·numbers·using·list·comprehension¬squared_numbers·=·[·············]¬¬#·Display·the·new·list·of·squared·numbers¬print("New·list·of·squared·numbers:",··)¬¶
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()0 00 10 20 0 0 0 1 2Infinite loopError
A curious young student named Liam stumbled upon a mathematical problem that computes the sum of the squares of its digits.Help Liam compute the result by writing a program using a do-while loop.Note: This question helps in clearing AMCAT exam.Input format :The input consists of a positive integer n.Output format :The output displays the result representing the sum of the squares of its digits.
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.