Asked Jun 24, 2019

Question: Write a function to display a pattern as follows:


2       1



n       n-1             2       1


Book: Introduction to programming using Python

Author: Y. Daniel Liang

Question: Chapter6, Page204, Question: 6.6


Expert Answer

Step 1


The python program has the following algorithm:

In the displayPattern() function,

  • Declare and initialize the required variables.
  • The for loop executes until the “n” number. If yes,
    • Calculate the result
    • Display the pattern.

Call the displayPattern() function by passing "n" value.

Step 2

Screenshot of program:


Image Transcriptionclose

#Define the function def displavPattern (n) #Declare and initialize the required variables result = 0 0 mult 1 #Loop executes until the "n" number for iin range (n) #Calculate the result (i+1) old result mult #Display the pattern print (result, end=' ') print #Multiply "10" with "mult" mult*10 #Assign the result to "old" variable old result = #Call the displayPattern () function by passing "n" value displayPattern (8)

Step 3

Editable code:

#Define the function

def displayPattern(n):


    #Declare and initialize the required variables

    result = 0

    old = 0

    mult = 1


    #Loop executes until the "n" number

    for i in range(n):


        #Calculate the result

        result = mult * (i+1) + old


        #Display the pattern


Want to see the full answer?

See Solution

Check out a sample Q&A here.

Want to see this answer and more?

Solutions are written by subject experts who are available 24/7. Questions are typically answered within 1 hour.*

See Solution
*Response times may vary by subject and question.
Tagged in


Computer Science


Related Computer Science Q&A

Find answers to questions asked by student like you
Show more Q&A

Q: Is the following distributive law valid or invalid? Prove your answer. x XOR (y + z) = (x XOR y) + ...

A: Answer:Here, the truth table is used to get the answer for each side of the question.First take LHS,


Q: CHALLENGE 1.24: Read multiple user inputs. ACTIVITY Write two scnr.nextint statements to get input v...

A: Given below is the program to get input values for month and year and write the output value as a mo...


Q: Write a program called Convert, which converts an ASCII code to its equivalentcharacter. 1) Create a...

A: Java codeimport java.util.Scanner; public class Convert{public static void main(String[] args) {Scan...


Q: Use Eclipse for do this program. Show the output of the following statements.a) System.out.printf("a...

A: The program has the following algorithm:a) The statement print the amount in floating point and expo...


Q: Write a complete C++ program that does the following:1. Create a 2-dimensional array with 8 rows and...

A: 1)Program plan:Declare the variables “rows” and “cols” as integerAssign “rows” to “8” and “cols” to ...


Q: How can I check if a binary number is an even number. So, the binary has a 0 remainder when divided ...

A: Approach: Result = (((n >> 1) << 1) == n). First we shift the 1 bit right then we shift ...


Q: Use Draw.IO to create an ERD based on the following information:• The RDBMS wou...

A: Below is the design of the Employees entity:- 


Q: (The Rectangle class) Following the example of the Circle class in Section 8.2,design a class named ...

A: Note: The given program is compiled and executed in "NetbeansIDE" 


Q: Write a complete C++ program that randomly generates 20 integers between the rangeof 1 to 50 and sto...

A: The random number is generated by using rand() function from stdlib.h and in order to generate a new...