Computer Science

What is following code doing? What would it print for input as 3?

n = int(input( "Enter an integer:" ))
if n < 1 :
  print ("invalid value")
else :
  for i in range(1, n + 1):
    print (i * i)

Python

Python Control Flow

35 Likes

Answer

The code will print the square of each number from 1 till the number given as input by the user if the input value is greater than 0. Output of the code for input as 3 is shown below:

Enter an integer:3
1
4
9

Answered By

17 Likes


Related Questions