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
- Alternating Star and Hash Triangle output
- How the alternating star and hash triangle works
- Alternating Star and Hash Triangle in C
- Alternating Star and Hash Triangle in C++
- Alternating Star and Hash Triangle in Java
- Alternating Star and Hash Triangle in Python
- Time and space complexity
- Frequently asked questions
Alternating Star and Hash Triangle output
Output (n = 5):
* *# *#* *#*# *#*#*

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.
- Run the outer loop for
ifrom1ton, one row per pass. - Go through
ipositions in the row. Print stars wherej % 2 = 1; print#symbols everywhere else. - Move to the next line.
Key takeaways
- The remainder test
j % 2picks the character. - Each column keeps one character from top to bottom.
- Using
(i + j) % 2gives 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.






