Python Program Print Cross of Stars – To print a cross of stars in Python, you can use nested loops to control the rows and columns of the cross. Here’s a simple program to print a cross of stars:
Source Code
# Get the size of the cross from the user - odd number size = int(input("Enter the size of the cross: ")) # Print the cross of stars for i in range(size): for j in range(size): if i == size // 2 or j == size // 2: print("*", end=" ") else: print(" ", end=" ") print()
Output
Enter the size of the cross: 7 * * * * * * * * * * * * *
In this program:
- We ask the user to enter the size of the cross.
- We use two nested
for
loops to iterate through the rows and columns of the cross. - We check if the current row (i) or column (j) is in the middle of the cross (size // 2), and if it is, we print a star (*); otherwise, we print a space (” “).
- After each row is printed (the inner loop completes), we use
print()
to move to the next line.
The result is a cross shape made of stars. You can adjust the size
variable to control the size of the cross.