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

Dot-Padded Star Triangle (…*, ..**)

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
  1. Dot-Padded Star Triangle output
  2. How the dot-padded star triangle works
  3. Dot-Padded Star Triangle in C
  4. Dot-Padded Star Triangle in C++
  5. Dot-Padded Star Triangle in Java
  6. Dot-Padded Star Triangle in Python
  7. Time and space complexity
  8. Frequently asked questions

Dot-Padded Star Triangle output

Output (n = 4):

...*
..**
.***
****
Dot-Padded Star Triangle layout diagram, row by row (n = 4)
How each row of the dot-padded star triangle is built (n = 4). Blue cells are printed characters; grey dots are spaces.

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.

  1. Run the outer loop for i from 1 to n, one row per pass.
  2. Print n − i . symbols.
  3. Print i stars.
  4. Move to the next line.

Key takeaways

  • Dots make the padding visible.
  • Dots and stars together always fill n characters.
  • 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.

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 16 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.