CBSE Class 11 Computer Science Question 98 of 161

Flow of Control — Question 7

Back to all questions
7
Question

Question 7

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)

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
Answer