Print the squares of all positive integers not exceeding n in ascending order.
Input. One positive integer n (n ≤
109).
Output. Print the list of squares of all positive integers not exceeding n in
ascending order.
Sample input 1 |
Sample output 1 |
10 |
1 4 9 |
|
|
Sample input 2 |
Sample output 2 |
20 |
1 4 9 16 |
loops
Use a for
or while
loop to print the
squares of numbers not exceeding n.
Read the input value of n.
scanf("%d",&n);
In the variable i iterate through the numbers 1, 2, 3,
… until i2 is less
than or equal to n. Sequentially print the squares of positive integers on a single line.
i = 1;
while(i * i <= n)
{
printf("%d ",i * i);
i++;
}
printf("\n");
Read the input value of n.
scanf("%d",&n);
In the variable i iterate through the numbers 1, 2, 3,
… until i2 is less
than or equal to n. Sequentially print the squares of positive integers on a single line.
for(i = 1; i * i <= n; i++)
printf("%d ",i * i);
printf("\n");
Python realization
Read the input value of n.
n = int(input())
In the variable i iterate through the numbers 1, 2, 3,
… until i2 is less
than or equal to n. Sequentially print the squares of positive integers on a single line.
i = 1
while i * i <= n:
print(i * i, end=" ")
i += 1