Python program to print square inside a square

The program starts by asking the user for the size of the outer square. Then, it uses nested loops to iterate through each position in the square. For each position, it checks if it is on the border of the square or inside the inner square. If it is on the border, it prints a *. If it is inside the inner square, it also prints a *. Otherwise, it prints a space.

The conditions to check if a position is inside the inner square are based on the size of the outer square. The inner square is one-third the size of the outer square, so the inner square starts at size//3 and ends at size-size//3-1 in both the rows and columns.

Given a number N, print a hollow square of side with N asterisks(‘*’), and inside it print a hollow square of side N-4 asterisks(‘*’).

Sure, here is a Python program to print a square inside a square using nested loops: 

 

size = int(input("Enter the size of the outer square: "))

# Outer loop for rows
for i in range(size):
    # Inner loop for columns
    for j in range(size):
        # Check if current position is on the border of the square
        if i == 0 or i == size-1 or j == 0 or j == size-1:
            print("*", end="")
        # Check if current position is inside the square
        elif i == size//3 and j >= size//3 and j <= size-size//3-1 or \
             i == size-size//3-1 and j >= size//3 and j <= size-size//3-1 or \
             j == size//3 and i >= size//3 and i <= size-size//3-1 or \
             j == size-size//3-1 and i >= size//3 and i <= size-size//3-1:
            print("*", end="")
        # Print a space for all other positions
        else:
            print(" ", end="")
    print()

 

Submit Your Programming Assignment Details