Please answer the attached question
Attachments:
Answers
Answered by
4
Required Answer:-
Question:
- Write a program to print the following pattern.
Solution:
Question 1:
Here is the program.
n=int(input("Enter the number of rows: "))
for i in range(n):
for j in range(i+1):
print("* ",end="")
print()
Explanation:
- We will ask the user to enter the number of rows. The first loop iterates n times(i = 0 to n). Inner loop iterates i+1 times. For example, when i=0, inner loop iterates 1 times and hence, one star is printed. After second iteration, two stars are printed and so on.
Sample I/O:
Enter the number of rows: 5
*
* *
* * *
* * * *
* * * * *
Question 2:
Here is the program.
n=int(input("Enter the number of rows: "))
for i in range(1,n+1):
for j in range(0,n-i):
print("",end=" ")
for j in range(0,i):
print("* ",end="")
print()
Explanation:
- Three loops are created. Outer loop iterates n times (number of rows). Inside the outer loop, two loops are created (for spaces and for stars). Each row has n - i number of spaces (n = number of rows and i = row number). After spaces are displayed, stars will be displayed.
Sample I/O:
Enter the number of rows: 5
*
* *
* * *
* * * *
* * * * *
Attachments:
Answered by
2
A. for i in range(1, 6):
print("*" * i)
B. for i in range(1, 6):
print(" " * (5 - i), "*" * i)
Similar questions