Practicing Star Pattern Problems



Star pattern problems (or questions) are the most common type of questions asked in exams and interviews. Infact not only in python, but they make a plenty amount of questions in any other language too like in C, Java etc. Thus, Practicing them is very important to Ace the exams and interviews. Let us see some of them to get confident about them and to tackle them properly. 

1. The Square Pattern

This is the simplest type of star pattern problem. In these problems, you will be given the number of stars in one side of the sqare (which will be the length of the square) and you have to print a square shape that would look like the following picture:-


The Code For The Problem:

n = int(input("Enter the length of square: "))
for i in range(n):
    print("* "*n)

2. The Right Triangle



In this problem we can see that the number of stars is increasing line-by-line. Thus a for loop could be use for solving the problem.
First we will take the input from user that how many rows he/she wants and after that we will execute the loop. 

The Code For The Problem:

n = int(input("Enter the no. of rows: "))
for i in range(1,n+1):
    print("* "*i)

3. The Equilateral Triangle

In these type of problems you will be given the number of stars in the side of the triangle and you will have to print something like this:- 


The approach is that you have to print some spaces and then a star and some spaces again. For this problem we can use the nested for loop.

The Code For The Problem:

n = int(input("Enter the no. of rows: "))
l = 2*n - 1
for i in range(n):
    for j in range(l):
      print(end=" ")
    l = l-1
    for k in range(i+1):
      print("*",end=" ")
    print("")

4. The Diamond

This will have a shape that is similar to the indian Burfi. The approach is that the number of stars will continue to increase progressively and after reaching a certain line, they will start decreasing progressively and at a certain line, it will end. The resultant will look like the following picture:-

The Code For The Problem:

n = int(input("Enter the no. of rows: "))
for i in range(n):
    for j in range(n - i - 1):
        print(' ', end='')
    for j in range(2 * i + 1):
        print('*', end='')
    print()
for i in range(n - 1):
    for j in range(i + 1):
        print(' ', end='')
    for j in range(2*(n - i - 1) - 1):
        print('*', end='')
    print()

5. The Hourglass


We can clearly see that this has an inverse relationship with the past one which was the diamond. The number of stars progressively decreases and reaches one and again it starts increasing. Thus the approach to overcome the problem would be similar to the past one but somehow, it will be different.

The Code For The Problem:

n  = int(input('Enter the no. of rows: '))
for i in range(n-1):
    for j in range(i):
        print(' ', end='')
    for k in range(2*(n-i)-1):
        print('*', end='')
    print()
for i in range(n):
    for j in range(n-i-1):
        print(' ', end='')
    for k in range(2*i+1):
        print('*', end='')
    print()

6. The Right Pascal Triangle

Have you ever seen the Pascal Triangle? It have a shape similar to the equilateral triangle. We have seen how to make a equilateral triangle out of stars. But now let us make a triangle which have its base in the left side of the screen. Basically, this figure will look like the following picture:-

The Code For The Problem:

n = int(input('Enter the no. of rows: '))
for i in range(n):
    print("*"*(i+1))
for i in range(n):
    print("*"*(n-i-1))


With that's it, I would like to end up this blog. For any type of query, question or suggestions you can comment down. See you in my next blog. Ba-Bye.

0 Comments