Star pattern programs are a common exercise in programming, particularly in learning loops and conditional statements. These programs involve printing patterns of stars (*) or other characters in different shapes and formations. They are a great way to practice logical thinking and enhance programming skills.
Star patterns can be created using loops (such as for loops or while loops) and conditional statements (such as if-else statements) to control the number of rows and the placement of stars and spaces within each row. By manipulating the loop variables and using conditional statements, we can generate various patterns, including triangles, pyramids, diamonds, and more.
Star pattern programs often involve nested loops, where an outer loop controls the rows, and an inner loop controls the columns within each row. The number of iterations in the loops determines the size of the pattern, while the conditional statements control the placement of stars and spaces.
Here are 10 different star (*) pattern programs in Python:
Pattern 1: Right Triangle
*
* *
* * *
* * * *
* * * * *
n = int(input("Enter the number of rows: "))
for i in range(1, n+1):
print('* ' * i)
This pattern prints a right triangle of stars. The number of rows in the triangle is taken as input from the user. Starting from the first row, each subsequent row has one more star than the previous row.
Pattern 2: Inverted Right Triangle
* * * * *
* * * *
* * *
* *
*
n = int(input("Enter the number of rows: "))
for i in range(n, 0, -1):
print('* ' * i)
Pattern is similar to Pattern 1, but it is inverted. The number of rows is taken as input, and the printing starts from the highest number of stars and decreases by one in each subsequent row.
Pattern 3: Hollow Right Triangle
*
* *
* *
* *
* * * * *
n = int(input("Enter the number of rows: "))
for i in range(1, n+1):
if i == 1 or i == n:
print('* ' * i)
else:
print('* ' + ' ' * (i-2) + '*')
This pattern is also a right triangle, but with a hollow interior. It prints a right triangle with stars on the edges and spaces inside. The first and last rows have stars throughout, while the intermediate rows have stars at the beginning and end, with spaces in between.
Pattern 4: Diamond
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
n = int(input("Enter the number of rows: "))
for i in range(1, n+1):
print(' ' * (n-i) + '* ' * i)
for i in range(n-1, 0, -1):
print(' ' * (n-i) + '* ' * i)
This pattern prints a diamond shape with stars. The number of rows is taken as input. It consists of an upside-down triangle followed by a regular triangle. The number of stars in each row increases until the middle row and then decreases. Spaces are used to align the stars properly.
Pattern 5: Pyramid
*
* *
* * *
* * * *
* * * * *
n = int(input("Enter the number of rows: "))
for i in range(1, n+1):
print(' ' * (n-i) + '* ' * i)
This pattern prints a pyramid shape with stars. The number of rows is taken as input, and the number of stars in each row increases from top to bottom.
Pattern 6: Hollow Pyramid
*
* *
* *
* *
* * * * *
n = int(input("Enter the number of rows: "))
for i in range(1, n+1):
if i == 1 or i == n:
print(' ' * (n-i) + '* ' * i)
else:
print(' ' * (n-i) + '* ' + ' ' * (i-2) + '*')
Similar to Pattern 5, this pattern prints a pyramid shape with stars. However, it includes a hollow interior. The first and last rows have stars throughout, while the intermediate rows have a star at the beginning and end, with spaces in between.
Pattern 7: Diamond with Hollow Center
*
* *
* *
* *
* * * * *
* *
* *
* *
*
n = int(input("Enter the number of rows: "))
print(' ' * (n-1) + '*') # Print the topmost row
# Print the upper half of the diamond
for i in range(2, n):
print(' ' * (n-i) + '*' + ' ' * (2*i-3) + '*')
# Print the middle row
print('* ' * (n))
# Print the lower half of the diamond
for i in range(n-1, 1, -1):
print(' ' * (n-i) + '*' + ' ' * (2*i-3) + '*')
print(' ' * (n-1) + '*') # Print the bottommost row
This pattern is a diamond shape, but with a hollow interior. It consists of an upside-down triangle followed by a regular triangle, just like Pattern 4. However, the interior stars are replaced with spaces.
Pattern 8: Left Triangle
* * * * *
* * * *
* * *
* *
*
n = int(input("Enter the number of rows: "))
for i in range(n, 0, -1):
print('* ' * i)
This pattern is similar to Pattern 1 but is a left-aligned triangle. The number of rows is taken as input, and each row has a decreasing number of stars from left to right.
Pattern 9: Hollow Left Triangle
* * * * *
* *
* *
* *
*
n = int(input("Enter the number of rows: "))
for i in range(n, 0, -1):
if i == 1 or i == n:
print('* ' * i)
else:
print('* ' + ' ' * (i-2) + '*')
Similar to Pattern 8, this pattern is a left-aligned triangle with a hollow interior. The first and last rows have stars throughout, while the intermediate rows have a star at the beginning and end, with spaces in between.
Pattern 10: Upside-Down Pyramid
* * * * *
* * * *
* * *
* *
*
n = int(input("Enter the number of rows: "))
for i in range(n, 0, -1):
print(' ' * (n-i) + '* ' * i)
This pattern is the opposite of Pattern 5. It prints an upside-down pyramid shape with stars. The number of stars in each row decreases from top to bottom.
You can run the respective code for each pattern to see how they look and modify them as per your requirements.