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
forloop sets up a loop that will iterate through the values ofifrom 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
forloop is nested inside the firstforloop, and sets up a loop that will iterate through the values ofjfrom 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 raisingito the power ofj. Thestr()function is used to convert the integer values ofiandjto 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 raiseito 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
iraised to the power ofjfor a particular combination ofiandj.
