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
What is the output of the following lines of code?
if int('zero') == 0 : print ("zero") elif str(0) == 'zero' : print (0) elif str(0) == '0' : print (str(0)) else: print ("none of the above")
Find the errors in the code given below and correct the code:
if n == 0 print ("zero") elif : n == 1 print ("one") elif n == 2: print ("two") else n == 3: print ("three")
How are following two code fragments different from one another? Also, predict the output of the following code fragments :
(a)
n = int(input( "Enter an integer:" )) if n > 0 : for a in range(1, n + n ) : print (a / (n/2)) else : print ("Now quiting")
(b)
n = int(input("Enter an integer:")) if n > 0 : for a in range(1, n + n) : print (a / (n/2)) else : print ("Now quiting")
Rewrite the following code fragment using for loop:
i = 100 while (i > 0) : print (i) i -= 3