The image below contains some question for printing different shapes.
Solutions:
Code for Question 11:
Alternative Method:
Code for Question 12:
Alternative Method:
Code for Question 13:
Alternative Method:
Code for Question 14:
Alternative Method:
Code for Question 11:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
width = int(input("Enter width of the box: ")) | |
height = int(input("Enter height of the box: ")) | |
for i in range(height): | |
for j in range(width): | |
if i == 0 or i == height-1: | |
print(end="*") | |
elif j > 0 and j < width-1: | |
print(end=" ") | |
else: | |
print(end="*") | |
print() |
Alternative Method:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
width = int(input("Enter width of the box: ")) | |
height = int(input("Enter height of the box: ")) | |
print("*"*width) | |
for i in range(height-2): | |
print("*" + " "*(width-2) + "*") | |
print("*"*width) |
Code for Question 12:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
height = int(input("Enter height of the triangle: ")) | |
for i in range(1,height+1): | |
for j in range(i): | |
print(end="*") | |
print() |
Alternative Method:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
height = int(input("Enter height of the triangle: ")) | |
for i in range(1,height+1): | |
print("*"*i) |
Code for Question 13:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
height = int(input("Enter height of the triangle: ")) | |
for i in range(height,0,-1): | |
for j in range(i): | |
print(end="*") | |
print() |
Alternative Method:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
height = int(input("Enter height of the triangle: ")) | |
for i in range(height,0,-1): | |
print("*"*i) |
Code for Question 14:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
height = int(input("Enter height of the triangle: ")) | |
mid = math.ceil(height/2) | |
for i in range(1,height+1): | |
if i > mid: | |
m = i-2*(i-mid) | |
else: | |
m = i | |
n = 2*m-1 | |
for j in range(mid-m): | |
print(end=" ") | |
for j in range(n): | |
print(end="*") | |
print() |
Alternative Method:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
height = int(input("Enter height of the triangle: ")) | |
mid = math.ceil(height/2) | |
for i in range(1,height+1): | |
if i > mid: | |
m = i-2*(i-mid) | |
else: | |
m = i | |
n = 2*m-1 | |
print(" "*(mid-m), end="") | |
print("*"*n) |
No comments:
Post a Comment