Question

In: Computer Science

pyramid.py program for i in range(1,6): for j in range(i): print("*",end=' ') print("\n",end='') Exercise: Create the...

pyramid.py program

for i in range(1,6):

for j in range(i):

print("*",end=' ')

print("\n",end='')

Exercise: Create the pyramid similar to the above pyramid.py program using a while loop. Name your program whilepyramid.py and submit it on blackboard.

Solutions

Expert Solution

Here is the code for the given question using while loop:

whilepyramid.py

*********************************************************************************************************************************************

i = 1 #initialising a variable i to 1 as we have to print in range 1 to 6 so start with 1
while i < 6: #continue the loop untill i is less than 6 as ending range is 6
j = 1 #initialising a variable j to 1
while j <= i: #checking if j is less than or equal to i   
print("*",end=' ') #print statement for a row
j+=1 #increment j
print("\n",end = '') #print statement for outer loop i.e change row
i+=1 #increment i

**********************************************************************************************************************************************

Providing screenshot of the code for further clarification

Output of my code :

**********************************************************************************************************************************************

For your verification i have provided your code that is by the for loop with its output:

for loop code:

pyramid.py

Output of this above code


Related Solutions

2) create a python program that uses a for loop and range to print out the...
2) create a python program that uses a for loop and range to print out the values 10 8 6 4 2 3) Create a python program that yses a for loop to print out ["bob","al","bert"]
I need it in java. Write a program that will print if n numbers that the...
I need it in java. Write a program that will print if n numbers that the user will input are or not within a range of numbers. For that, your program needs to ask first for an integer number called N that will represent the number of times that will ask for other integer numbers. Right after, it should ask for two numbers that will represent the Min and Max for a range. Lastly. it will iterate N number times...
1. Create a program that generates n values in range [low, high]. • Create a function...
1. Create a program that generates n values in range [low, high]. • Create a function generate_vals which takes as input an array of double values and three integers representing the size of the array, low value of range, and high value of range, respectively. The function should use rand() to generate the requested number of values. • Prompt the user to enter the size of their array as shown below. It cannot exceed a maximum size of 128. If...
for (i=0; i<n; i++) for (j=1; j<n; j=j*2)    for (k=0; k<j; k++) ... // Constant...
for (i=0; i<n; i++) for (j=1; j<n; j=j*2)    for (k=0; k<j; k++) ... // Constant time operations end for end for end for Analyze the following code and provide a "Big-O" estimate of its running time in terms of n. Explain your analysis. Note: Credit will not be given only for answers - show all your work: (2 points) steps you took to get your answer. (1 point) your answer.
int f2 (int n) j = 0; while (j <n) {for (int i = 0; i...
int f2 (int n) j = 0; while (j <n) {for (int i = 0; i <n; ++ i) {cout << "j =" + j; j = j + 5; }}
n this exercise, you create a program for the sales manager at Computer Haven, a small...
n this exercise, you create a program for the sales manager at Computer Haven, a small business that offers motivational seminars to local companies. Figure 7-53 shows the charge for attending a seminar. Notice that the charge per person depends on the number of people the company registers. For example, the cost for four registrants is $400; the cost for two registrants is $300. The program should allow the sales manager to enter the number of registrants for as many...
def longest(string): start=0;end=1;i=0; while i<len(string): j=i+1 while j<len(string) and string[j]>string[j-1]: j+=1 if end-start<j-i: #update if current...
def longest(string): start=0;end=1;i=0; while i<len(string): j=i+1 while j<len(string) and string[j]>string[j-1]: j+=1 if end-start<j-i: #update if current string has greater length than #max start and end end=j start=i i=j; avg=0 for i in string[start:end]: avg+=int(i) print('The longest string in ascending order is',string[start:end]) print('Teh average is',avg/(end-start)) s=input('Enter a string') longest(s) i need a definition and explanation of this code that how it works?
Given an array A[1..n] of integers such that A[j] ≥ A[i] − d for every j...
Given an array A[1..n] of integers such that A[j] ≥ A[i] − d for every j > i. That means any inversion pair in A differs in value by at most d. Give an O(n + d) algorithm to sort this array.
The program must prompt for an integer divisor in the range 10-20 and will print all...
The program must prompt for an integer divisor in the range 10-20 and will print all numbers 1-1000 that are divisible by that divisor. You MUST use a while loop for this. Use printf to display the numbers in right-aligned columns, 10 to a line. For example, “%5d” would be the format String to allow a field width of 5 to display the number. If the number entered by the user is out of range just print an error message....
PYTHON Let n denote an integer entered by the user. Write a program to print n...
PYTHON Let n denote an integer entered by the user. Write a program to print n multiples of 5 in the descending order, with the last number being 5. Print the average of those n multiples
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT