Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a nested for loop that prints the following output: 1 1 2 1 1 2 4 2 1 1 2

ID: 3626323 • Letter: W

Question

Write a nested for loop that prints the following output:

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

Hint: Here is the psudocode solution:

for row from 0 to 7 {
pad leading blanks in a row like this
for the column from 1 to 7-row:
print a set of spaces
print left half of row
for column from 0 to row:
print spaces followed by 2 raised to the power of the column
print right half of row
for column from row-1 to 0:
print spaces followed by 2 raised to the power of the column
print a new line

You need to figure out how many spaces to print before the number. The simple way to compute powers of 2 is with the pow function; can you do this without it?

Explanation / Answer

#include int main() { int i,j,k=0; for(i=0;i
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote