Python program to print arrow pattern

Python Arrow Pattern Program
Python Arrow Pattern Program

Problem Statement

Print an arrow star pattern for a given number of rows N. Note: N is always odd, and there is a space after every star.

Input Format

Integer N

Output Format

The arrow pattern in N lines

Examples

Sample Input 1 :

7

Sample Output 1 :


*
 * *
   * * *
     * * * *
   * * *
 * *
*

Sample Input 2 :

11

Sample Output 2 :


*
 * *
   * * *
     * * * *
       * * * * *
         * * * * * *
       * * * * *
     * * * *
   * * *
 * *
*

Solution Approach

Steps to solve:

  1. Read the number of rows N.
  2. Split the problem into two halves: - Upper half including the middle row - Lower half (reverse of upper half without the middle row)
  3. Print spaces followed by stars with one space after each star.
  4. Ensure symmetry for correct arrow shape.

Python Code


N = int(input())

n1 = (N + 1) // 2
n2 = N - n1

# Upper half
i = 1
while i <= n1:
    print(' ' * (i - 1) + '* ' * i)
    i += 1

# Lower half
i = 1
while i <= n2:
    print(' ' * (n2 - i) + '* ' * (n2 - i + 1))
    i += 1

Try it Online

Previous Post Next Post