New: 203 pattern programs with tested code in C, C++, Java and Python Explore patterns

Up Arrow Star Pattern

Read summary with AI tools

The up arrow star pattern is a pyramid with a straight shaft underneath it, so the whole shape points up. It joins two very simple pieces, and the only care needed is lining the shaft up with the tip.

Quick answer: Print a pyramid for the head: row i has n − i spaces and 2i − 1 stars. Then print the shaft: n − 1 rows, each with n − 1 spaces and one star, which puts the star in the same column as the tip.

This program is part of the Star Patterns collection.

Table of contents8 sections
  1. Output
  2. How the up arrow star pattern works
  3. Program in C
  4. Program in C++
  5. Program in Java
  6. Program in Python
  7. Time and space complexity
  8. Frequently asked questions

Up Arrow Star Pattern output

Output (n = 5):

    *
   ***
  *****
 *******
*********
    *
    *
    *
    *
Up Arrow Star Pattern layout diagram, row by row (n = 5)
How each row of the up arrow star pattern is built (n = 5). Blue cells are printed characters; grey dots are spaces.

How the up arrow star pattern works

The tip of the pyramid sits in column n, after n − 1 spaces. The shaft just repeats that single star for a few rows, so every shaft row uses the same n − 1 spaces. Choosing n − 1 shaft rows keeps the arrow in proportion, but any length works.

  1. Run the outer loop for i from 1 to n, one row per pass (arrow head: a pyramid).
  2. Print n − i spaces.
  3. Print 2 × i − 1 stars.
  4. Move to the next line.
  5. Run the outer loop for i from 1 to n − 1, one row per pass (shaft: one centred star per row).
  6. Print n − 1 spaces.
  7. Print 1 stars.
  8. Move to the next line.

Key takeaways

  • The arrow is a pyramid plus a straight shaft.
  • The shaft star is indented n − 1 spaces, the same as the tip.
  • The shaft length is free; n − 1 rows looks balanced.
  • Swap the order of the two parts and use an inverted pyramid for a down arrow.

Up Arrow Star Pattern in C

#include <stdio.h>

int main(void) {
    int n = 5;

    // arrow head: a pyramid
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n - i; j++)
            printf(" ");
        for (int j = 1; j <= 2 * i - 1; j++)
            printf("*");
        printf("\n");
    }

    // shaft: one centred star per row
    for (int i = 1; i <= n - 1; i++) {
        for (int j = 1; j <= n - 1; j++)
            printf(" ");
        for (int j = 1; j <= 1; j++)
            printf("*");
        printf("\n");
    }
    return 0;
}

Up Arrow Star Pattern in C++

#include <iostream>
using namespace std;

int main() {
    int n = 5;

    // arrow head: a pyramid
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n - i; j++)
            cout << " ";
        for (int j = 1; j <= 2 * i - 1; j++)
            cout << "*";
        cout << endl;
    }

    // shaft: one centred star per row
    for (int i = 1; i <= n - 1; i++) {
        for (int j = 1; j <= n - 1; j++)
            cout << " ";
        for (int j = 1; j <= 1; j++)
            cout << "*";
        cout << endl;
    }
    return 0;
}

Up Arrow Star Pattern in Java

public class UpArrowStar {
    public static void main(String[] args) {
        int n = 5;

        // arrow head: a pyramid
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n - i; j++)
                System.out.print(" ");
            for (int j = 1; j <= 2 * i - 1; j++)
                System.out.print("*");
            System.out.println();
        }

        // shaft: one centred star per row
        for (int i = 1; i <= n - 1; i++) {
            for (int j = 1; j <= n - 1; j++)
                System.out.print(" ");
            for (int j = 1; j <= 1; j++)
                System.out.print("*");
            System.out.println();
        }
    }
}

Up Arrow Star Pattern in Python

n = 5

# arrow head: a pyramid
for i in range(1, n + 1):
    for j in range(1, n - i + 1):
        print(" ", end="")
    for j in range(1, 2 * i):
        print("*", end="")
    print()

# shaft: one centred star per row
for i in range(1, n):
    for j in range(1, n):
        print(" ", end="")
    for j in range(1, 2):
        print("*", end="")
    print()

Time and space complexity

The program prints one character for each position in the pattern, so for n rows it runs in O(n²) time. It only needs a few loop variables, so the extra memory is O(1).

Frequently asked questions

How do I make the shaft thicker?

Print three stars per shaft row with n − 2 spaces in front. The shaft stays centred because it grows by one star on each side.

How do I point the arrow down?

Print the shaft first and then an inverted pyramid, as in the down arrow star pattern.

Can I draw left and right arrows too?

Yes, the site has a right arrow and a left arrow. They grow and shrink along the rows instead of the columns.

Authored by

Ghanendra Yadav runs Pattern Programs from Bangalore, India. The site started in 2020 to help students practise loops and logic through pattern programs in C, C++, Java and Python. Every program published here is compiled and run before it goes live, and the output shown is the real output.

Latest articles

Leave a comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.