Question

In: Computer Science

must use python Write a nested for loop that displays the following output using the same...

  1. must use python
  2. Write a nested for loop that displays the following output using the same integer from part a:

                                                    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

Solutions

Expert Solution

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 :)


Related Solutions

Python Write a for loop with a range function and format output as currency Use an...
Python Write a for loop with a range function and format output as currency Use an input statement to ask the user for # of iterations using the prompt: #? [space after the ?] & assign to a variable Convert the variable to an integer Use a for loop and a range function to display all whole numbers from 1 to the user entered number Display the value of the item variable on screen in each iteration in the following...
In Java: Write a nested loop that allows the user to continuously enter phrases and output...
In Java: Write a nested loop that allows the user to continuously enter phrases and output underscores for each character of the phrase. End when the user enters "done" (ignoring case). Once you have this completed, modify your code so that if the character is whitespace, you print a space " " instead of an underscore - Hint: use Character.isWhitespace(YOURCHARHERE) //returns a boolean.
The following code must be written using matlab and must be using a for-loop. NOTE! Write...
The following code must be written using matlab and must be using a for-loop. NOTE! Write a computer program that assigns random integers for each entry and generates a row vector. Different random integers should be drawn from different intervals for position 1, position 2, position3 and position 4 of the array. After these first 4 positions are drawn. The whole thing should start over where position5 drawn from same interval as positions 1, position6 drawn from same interval as...
The following code must be written using matlab and must be using a for-loop. NOTE! Write...
The following code must be written using matlab and must be using a for-loop. NOTE! Write a computer program that assigns random integers for each entry and generates a row vector. Different random integers should be drawn from different intervals in chunks of 4 , that is chunk1-chunk2-chunk3-chunk4 The parameters for specifying the lintervals by which the random numbers should be drawn should be able to change and be hardcoded in the script, however, be hardcoded in the script.
Write a python code which prints triangle of stars using a loop ( for loop )...
Write a python code which prints triangle of stars using a loop ( for loop ) Remember what 5 * "*" does The number of lines of output should be determined by the user. For example, if the user enters 3, your output should be: * ** *** If the user enters 6, the output should be: * ** *** **** ***** ****** You do NOT need to check for valid input in this program. You may assume the user...
Important: please use python. Using while loop, write python code to print the times table (from...
Important: please use python. Using while loop, write python code to print the times table (from 0 to 20, incremented by 2) for number 5. Add asterisks (****) so the output looks exactly as shown below.   Please send the code and the output of the program. ****************************************************************** This Program Shows Times Table for Number 5 (from 0 to 20) Incremented by 2 * ****************************************************************** 0 x 5 = 0 2 x 5 = 10 4 x 5 = 20 6...
Use PYTHON --Nested Lists and DateTime Module. Write a program that does the following: Reads information...
Use PYTHON --Nested Lists and DateTime Module. Write a program that does the following: Reads information from a text file into a list of sublists. Be sure to ask the user to enter the file name and end the program if the file doesn’t exist. Text file format will be as shown, where each item is separated by a comma and a space: ID, firstName, lastName, birthDate, hireDate, salary Store the information into a list of sublists called empRoster. EmpRoster...
Complete the following in syntactically correct Python code. Write a program, using a for loop, that...
Complete the following in syntactically correct Python code. Write a program, using a for loop, that calculates the amount of money a person would earn over a period of time if his or her salary is 1 penny for the first day, 2 pennies for the second day, 4 pennies for the third day, and continues to double each day. 1.      The program should ask the user for the number of days the employee worked. 2.      Display a table showing the salary...
Using Python, use the following list (Temperature = [56.2,31.8,81.7,45.6,71.3,62.9,59.0,92.5,95.0,19.2,15.0]) to: - Create a loop to iterate...
Using Python, use the following list (Temperature = [56.2,31.8,81.7,45.6,71.3,62.9,59.0,92.5,95.0,19.2,15.0]) to: - Create a loop to iterate through each of the elements in the temperature list. - Convert each element of this list to a Celsius temperature and then, for each valid temperature in the list, print out both the original Fahrenheit temperature and the Celsius equivalent in this format: "32 degrees Fahrenheit is equivalent with 0 degrees Celsius."
Please use Microsoft excel. Using a Nested Loop, what is the equation to solve for letter...
Please use Microsoft excel. Using a Nested Loop, what is the equation to solve for letter grade Nested loop Student ID Grade Letter Grade 1 5 If score is Then return 2 55 Greater than 89 A 3 86 From 80 to 89 B 5 64 From 70 to 79 C 6 25 from 60 to 69 D 7 56 less than 60 F 8 58 9 99 10 90 11 28
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT