The dot-padded star triangle is a right-aligned triangle where the empty space is shown with dots instead of blanks. Placement tests use it because dots make the leading spaces visible and easy to check.
Quick answer: Row i prints n − i dots and then i stars. Every row is n characters wide, so the stars line up on the right. Swap the dot for a space and it becomes the mirrored right triangle.
This program is part of the Symbol Patterns collection.
Table of contents8 sections
Dot-Padded Star Triangle output
Output (n = 4):
...* ..** .*** ****

Same shape, other characters: the Row Number Square program draws 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 dot-padded star triangle works
Leading spaces are invisible on screen, so mistakes in their count are easy to miss. Printing a dot instead shows exactly how many padding characters each row has. The counts are the same as in any right-aligned triangle: they add up to n on every row.
- Run the outer loop for
ifrom1ton, one row per pass. - Print
n − i.symbols. - Print
istars. - Move to the next line.
Key takeaways
- Dots make the padding visible.
- Dots and stars together always fill
ncharacters. - Changing the dot to a space gives the mirrored right triangle.
- Printing padding as dots is a handy way to debug any centred pattern.
Dot-Padded Star Triangle in C
#include <stdio.h>
int main(void) {
int n = 4;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++)
printf(".");
for (int j = 1; j <= i; j++)
printf("*");
printf("\n");
}
return 0;
}Dot-Padded Star Triangle in C++
#include <iostream>
using namespace std;
int main() {
int n = 4;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++)
cout << ".";
for (int j = 1; j <= i; j++)
cout << "*";
cout << endl;
}
return 0;
}Dot-Padded Star Triangle in Java
public class DotPaddedTriangle {
public static void main(String[] args) {
int n = 4;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++)
System.out.print(".");
for (int j = 1; j <= i; j++)
System.out.print("*");
System.out.println();
}
}
}Dot-Padded Star Triangle in Python
n = 4
for i in range(1, n + 1):
for j in range(1, n - i + 1):
print(".", end="")
for j in range(1, i + 1):
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
Why use dots instead of spaces?
Dots let you count the padding at a glance. Test setters use them so that answers with wrong spacing are easy to spot.
How do I count the dots on each row?
Row i has n − i dots: three on the first row of a four-row triangle and none on the last. The number of stars is always n minus the number of dots.
How do I make it a pyramid with dots?
Print n − i dots, 2i − 1 stars and another n − i dots. Each row is then 2n − 1 characters wide.
How do I let the user choose n?
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.






