Squares of numbers#8544

Squares of numbers (Editorial)

Use a for or while loop to print the squares of numbers not exceeding .

Algorithm realization

Read the input value of .

scanf("%d",&n);

In the variable iterate through the numbers 1, 2, 3, ... until i^2 is less than or equal to . Sequentially print the squares of positive integers on a single line.

i = 1;
while(i * i <= n)
{
  printf("%d ",i * i);
  i++;
}
printf("\n");

Algorithm realization — for loop

Read the input value of .

scanf("%d",&n);

In the variable iterate through the numbers 1, 2, 3, ... until i^2 is less than or equal to . 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 = int(input())

In the variable iterate through the numbers 1, 2, 3, ... until i^2 is less than or equal to . Sequentially print the squares of positive integers on a single line.

i = 1
while i * i <= n:
  print(i * i, end=" ")
  i += 1