68.9k views
0 votes
Given a variable n that is associated with a positive integer and a variable s that is associated with the empty string, write some statements that use a while loop to associate s with a string consisting of exactly 2n asterisks (*) . (So if n were associated with 4 then s would, in the end, be associated with "********" .

1 Answer

0 votes

Answer:

Here is the Python program:

n = int(input("Enter a positive integer n: "))

#prompts user to enter value of n and stores that value to n variable

s= 2*n # to associate 2n asterisks

asterisks = '*' # used to display asterisks

while n>0: # loop continues until n is less than or equal to 0

print(asterisks*s) #prints 2n asterisks

n = n-s # decrements value of n by s

Explanation:

This program prompts user to enter value of n. n has the positive integer value and s holds 2*n to associate 2n asterisks. asterisks variable hold asterisk that is to be displayed 2n times. The loop continues to associate asterisks with s which is 2 times the value of n and displays these asterisks on output screen. The while loop breaks when value of n is less than 0. The output is displayed in screenshot.

Given a variable n that is associated with a positive integer and a variable s that-example-1
User Dcmorse
by
5.6k points
Welcome to QAmmunity.org, where you can ask questions and receive answers from other members of our community.