Python Program Print Cross of Stars

By | October 30, 2023

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:

Python Program Print CROSS of Stars

Python Program Print 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:

  1. We ask the user to enter the size of the cross.
  2. We use two nested for loops to iterate through the rows and columns of the cross.
  3. 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 (” “).
  4. 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.

Loading

Leave a Reply

Your email address will not be published. Required fields are marked *