In: Computer Science
1
1 2 1
1 2 4 2 1
1 2 4 8 4 2 1
1 2 4 8 16 8 4 2 1
1 2 4 8 16 32 16 8 4 2 1
1 2 4 8 16 32 64 32 16 8 4 2 1
1 2 4 8 16 32 64 128 64 32 16 8 4 2 1
from part a: Write a program that prompts the user to enter an integer from 1 to 15 and displays a pyramid, as shown in the following sample run:
Enter the number of lines: 7
1
2 1 2
3 2 1 2 3
4 3 2 1 2 3 4
5 4 3 2 1 2 3 4 5
6 5 4 3 2 1 2 3 4 5 6
7 6 5 4 3 2 1 2 3 4 5 6 7
ANSWER:
Code for 1st question :
height = int(input("Enter the Integer value : "))
maxHeight = height + 1
for i in range(maxHeight):
k, Max = 1, i * 2 + 1
print(maxHeight * " ", end="")
maxHeight -= 1
for j in range(Max):
print("%5d" % k, end="")
if j < (Max // 2):
k *= 2
else:
k //= 2
print()
Screenshot with output:
Code for 2nd question:
num = int(input("Enter the number of rows : "))
for i in range(1, num+1):
for j in range(1, num-i+1):
print(end=" ")
for j in range(i, 0 , -1):
print(j,end="")
for j in range(2, i+1):
print(j,end="")
print()
Screenshot with output:
So, that was the Full Python code and output with implementation screenshots using Nested for loops, as asked in the question.
Hope it helps, plz feel free to ask and clarify any doubts, if it did help plz consider putting a like, it would be really great of you, THANKS and have a great day :)