0% found this document useful (0 votes)
4 views

program 2

Uploaded by

qhinata656
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

program 2

Uploaded by

qhinata656
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Program No.

2:

To write the python program to construct the following pattern using a nested
loop:
*
**
***
****
*****
****
***
**
*

Model 1:

n=5

# Upper half of the diamond


for i in range(n):
for j in range(n - 1, i, -1):
print(" ", end="")
for k in range(i + 1):
print("* ", end="")
print()

# Lower half of the diamond


for i in range(1, n):
for j in range(i):
print(" ", end="")
for k in range(n - 1, i - 1, -1):
print("* ", end="")
print()

Output:
*
**
***
****
*****
****
***
**
*
Model 2:

To write the python program to construct the following pattern using a nested
loop:

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

n=5

for i in range(n):
for j in range(n):
if i == 0 or i == n - 1 or j == 0 or j == n - 1:
print("*", end="")
else:
print(" ", end="")
print()

Output:

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

You might also like