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

Alternating Star and Hash Triangle (*#*#)

The alternating star and hash triangle prints a right triangle where the characters take turns: a star, a hash, a star, and so on. ICSE question papers often ask a version of it with $ and #.

Quick answer: Use the usual right-triangle loops, row i having i positions. Print a star at odd positions and a hash at even positions: the test is j % 2 == 1. Every row starts with a star because position 1 is odd.

This program is part of the Symbol Patterns collection.

Table of contents8 sections
  1. Alternating Star and Hash Triangle output
  2. How the alternating star and hash triangle works
  3. Alternating Star and Hash Triangle in C
  4. Alternating Star and Hash Triangle in C++
  5. Alternating Star and Hash Triangle in Java
  6. Alternating Star and Hash Triangle in Python
  7. Time and space complexity
  8. Frequently asked questions

Alternating Star and Hash Triangle output

Output (n = 5):

*
*#
*#*
*#*#
*#*#*
Alternating Star and Hash Triangle layout diagram, row by row (n = 5)
How each row of the alternating star and hash triangle is built (n = 5). Blue cells are printed characters; grey dots are spaces.

Same shape, other characters: the Descending Number Triangle, Descending Repeated Number Triangle, Number Right Triangle, Repeated Number Triangle and Reverse Number Triangle programs draw this same outline with different characters. Putting them side by side shows which part of the loop decides the shape and which part decides what is printed.

How the alternating star and hash triangle works

The shape comes from the loops and the characters come from one remainder test. Because the test uses the column j, every column holds the same character all the way down: column 1 is all stars, column 2 all hashes. Testing (i + j) % 2 instead would make the rows alternate their starting character too.

  1. Run the outer loop for i from 1 to n, one row per pass.
  2. Go through i positions in the row. Print stars where j % 2 = 1; print # symbols everywhere else.
  3. Move to the next line.

Key takeaways

  • The remainder test j % 2 picks the character.
  • Each column keeps one character from top to bottom.
  • Using (i + j) % 2 gives a checkerboard-style triangle.
  • Swap the two symbols to start each row with a hash.

Alternating Star and Hash Triangle in C

#include <stdio.h>

int main(void) {
    int n = 5;

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= i; j++) {
            if (j % 2 == 1)
                printf("*");
            else
                printf("#");
        }
        printf("\n");
    }
    return 0;
}

Alternating Star and Hash Triangle in C++

#include <iostream>
using namespace std;

int main() {
    int n = 5;

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= i; j++) {
            if (j % 2 == 1)
                cout << "*";
            else
                cout << "#";
        }
        cout << endl;
    }
    return 0;
}

Alternating Star and Hash Triangle in Java

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

        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= i; j++) {
                if (j % 2 == 1)
                    System.out.print("*");
                else
                    System.out.print("#");
            }
            System.out.println();
        }
    }
}

Alternating Star and Hash Triangle in Python

n = 5

for i in range(1, n + 1):
    for j in range(1, i + 1):
        if j % 2 == 1:
            print("*", end="")
        else:
            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 rows alternate their first character?

Use (i + j) % 2 == 0 as the test. Row 1 then starts with a star, row 2 with a hash, and so on, like the checkerboard pattern cut into a triangle.

How do I print the ICSE $#$#$ version?

Replace the star with $. Everything else stays the same.

How do I let the user choose the number of rows?

Read n from the keyboard instead of fixing it in the code: scanf("%d", &n); in C, cin >> n; in C++, new Scanner(System.in).nextInt() in Java and int(input()) in Python.

This tutorial was drafted with AI assistance. Spotted a mistake? Tell us and we fix it. Read our editorial policy.

Quick check: can you predict the output?

Answer from the code, then check. Every answer comes from the real output above.

Pattern 8 of 17 in Symbol Patterns

 

Still stuck? Ask AI to explain this code

Opens your favourite AI with this page, ready to explain every loop step by step.

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.

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.