How can I reverse the value of the output?

Question:

Let’s put int 5 as a sample input; in my code its expressed as this:

n = int(input())

for i in range(0, n):
   n = n - 1
   print(n**2)

I get an output as:

16
9
4
1
0

Instead I want to reverse the result to:

0
1
4
9
16

How do you go about solving this problem?

Asked By: Joshua Partido

||

Answers:

The following will reverse the output:

n = int(input())
for i in range(0,n):
    print(i**2)

It will loop from 0 to your inputted n value, printing it’s square at each iteration. This also negates the need for the n = n - 1 line.

Output:

0 ** 2
1 ** 2
.
.
.
n ** 2
Answered By: roberthayek
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.