This program uses nested for loop to print powers of numbers from one to twenty. This program is a simple example of using nested loops in Python to perform a repetitive task, in this case computing and printing the values of number powers from 1 to 5 of numbers from1 to 20. Outer for loop iterate over numbers from 1 to 20 by using range function. Inner loop iterate over powers the number is being raised to, i.e. from 1 to 5.
Python Code:
for i in range(1,21):
for j in range(1,6):
print(str(i)+"^"+str(j)+"="+str(i**j))
This code uses nested for
loops to compute and print the values of i
raised to the power of j
for all values of i
from 1 to 20 and all values of j
from 1 to 5.
- The first
for
loop sets up a loop that will iterate through the values ofi
from 1 to 20, inclusive. This is done using therange()
function, which creates a sequence of numbers from the starting value (1) to the ending value (20) with a step size of 1. - The second
for
loop is nested inside the firstfor
loop, and sets up a loop that will iterate through the values ofj
from 1 to 5, inclusive. This is also done using therange()
function. - Inside the nested loops, the
print()
function is called, which will print the result of raisingi
to the power ofj
. Thestr()
function is used to convert the integer values ofi
andj
to strings so that they can be concatenated with the other strings. The+
operator is used to concatenate the strings together, and the**
operator is used to raisei
to the power ofj
. - The output of each iteration of the nested loops is printed to the console as a separate line. Each line of output represents the value of
i
raised to the power ofj
for a particular combination ofi
andj
.