Write a function that reads a number N (1 to 100) and prints all the perfect squares upto N.
Question
Write a function that reads a number N (1 to 100) and prints all the perfect squares upto N.
Solution
Sure, here is a Python function that does that:
def print_perfect_squares(N):
# Check if N is within the valid range
if N < 1 or N > 100:
print("Invalid input. Please enter a number between 1 and 100.")
return
# Loop through numbers from 1 to N
for i in range(1, N+1):
# If the square of a number is less than or equal to N, print it
if i*i <= N:
print(i*i)
else:
# If the square of a number is greater than N, break the loop
break
You can call this function with a number N as follows:
print_perfect_squares(
Similar Questions
Exercise 1-1: Calculate the sum of square of integers from 1 to 100 using a while loop. Output each integer (from 1 to 100) followed by its sum of squares up to that integer.Last 3 lines of output:...99 328350100 338350n_sum = 338350 when n = 100
Problem Statement:Given a number, check whether it is a perfect square or not.Input Format:Given an integer n.Output Format:Print Yes if it is a perfect square or No if it is not.Constraints1 <= n <= 10^6Sample Input 1:100Sample Output 1:YesSample Input 2:34Sample Output 2:No
Write a Python program to print the square of first 10 natural numbers, using while loop
Find the square of the number in the range 1-500 which is a perfect square. The number when turned upside down still results in a perfect square with the square root in the range 1-500. (Assuming numbers 1, 8, 0 remain same when turned upside down with 6 and 9 resulting in 9 and 6 in being turned upside down)
How many whole numbers between 39 and 160 are perfect squares
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.