⚡ TL;DR
Learn star, number, and character programming patterns in Dart with working code examples — squares, triangles, pyramids, Fibonacci, and palindromes explained.
What Are Programming Patterns?
In this article, “programming patterns” means pattern printing exercises — classic loop practice problems where you use nested loops to print shapes made of stars, numbers, or characters. (They are not the same thing as “design patterns” like Singleton or Factory, which solve code-architecture problems.) These exercises are a staple of programming interviews and coursework because they test your control over loops, string building, and spacing logic.
Every example below builds each row as a string and prints it once per row, and each snippet includes its expected output.
Star Patterns
Star patterns are the most common type of pattern printing exercise. They involve printing various shapes and designs using asterisks. Here are 5 examples of star patterns in Dart:
Square Pattern
void squarePattern(int n) {
for (int i = 1; i <= n; i++) {
String row = '';
for (int j = 1; j <= n; j++) {
row += '* ';
}
print(row);
}
}
void main() {
squarePattern(4);
}Output:
* * * *
* * * *
* * * *
* * * *This program prints a square pattern of n rows and n columns. The outer loop runs once per row; the inner loop appends a star to the row string n times.
Right Triangle Pattern
void rightTrianglePattern(int n) {
for (int i = 1; i <= n; i++) {
String row = '';
for (int j = 1; j <= i; j++) {
row += '* ';
}
print(row);
}
}
void main() {
rightTrianglePattern(4);
}Output:
*
* *
* * *
* * * *This program prints a right triangle pattern of n rows. Row i has exactly i stars.
Left Triangle Pattern (Right-Aligned)
void leftTrianglePattern(int n) {
for (int i = 1; i <= n; i++) {
String row = '';
// Print (n - i) leading spaces to push the stars to the right
for (int j = n; j > i; j--) {
row += ' ';
}
for (int k = 1; k <= i; k++) {
row += '* ';
}
print(row);
}
}
void main() {
leftTrianglePattern(4);
}Output:
*
* *
* * *
* * * *This program prints a right-aligned triangle pattern of n rows. Because the stars are space-separated, each leading gap is two spaces wide.
Diamond Pattern
void diamondPattern(int n) {
// Top half, including the middle row
for (int i = 1; i <= n; i++) {
String row = '';
for (int j = n; j > i; j--) {
row += ' ';
}
for (int k = 1; k <= (2 * i - 1); k++) {
row += '* ';
}
print(row);
}
// Bottom half
for (int i = n - 1; i >= 1; i--) {
String row = '';
for (int j = n; j > i; j--) {
row += ' ';
}
for (int k = 1; k <= (2 * i - 1); k++) {
row += '* ';
}
print(row);
}
}
void main() {
diamondPattern(4);
}Output:
*
* * *
* * * * *
* * * * * * *
* * * * *
* * *
*This program prints a diamond pattern with a widest row of n stars. Each row i has 2 * i - 1 stars.
Hourglass Pattern
void hourglassPattern(int n) {
// Narrow down to a single star
for (int i = n; i >= 1; i--) {
String row = '';
for (int j = n; j > i; j--) {
row += ' ';
}
for (int k = 1; k <= (2 * i - 1); k++) {
row += '* ';
}
print(row);
}
// Widen back out (skip the single-star row to avoid printing it twice)
for (int i = 2; i <= n; i++) {
String row = '';
for (int j = n; j > i; j--) {
row += ' ';
}
for (int k = 1; k <= (2 * i - 1); k++) {
row += '* ';
}
print(row);
}
}
void main() {
hourglassPattern(4);
}Output:
* * * * * * *
* * * * *
* * *
*
* * *
* * * *
* * * * * * *This program prints an hourglass pattern that narrows to one star and widens back out.
Number Patterns
Number patterns are another type of pattern printing exercise that involves printing various numerical designs. Here are 3 examples of number patterns in Dart:
Number Pyramid Pattern
void numberPyramidPattern(int n) {
for (int i = 1; i <= n; i++) {
String row = '';
// Leading spaces for the pyramid shape
for (int j = 1; j <= (n - i); j++) {
row += ' ';
}
for (int k = 1; k <= i; k++) {
row += '$k ';
}
print(row);
}
}
void main() {
numberPyramidPattern(4);
}Output:
1
1 2
1 2 3
1 2 3 4This program prints a number pyramid pattern of n rows, where row i contains the numbers 1 through i.
Number Diamond Pattern
void numberDiamondPattern(int n) {
// Top half, including the middle row
for (int i = 1; i <= n; i++) {
String row = '';
for (int j = n; j > i; j--) {
row += ' ';
}
for (int k = 1; k <= (2 * i - 1); k++) {
row += '$i ';
}
print(row);
}
// Bottom half
for (int i = n - 1; i >= 1; i--) {
String row = '';
for (int j = n; j > i; j--) {
row += ' ';
}
for (int k = 1; k <= (2 * i - 1); k++) {
row += '$i ';
}
print(row);
}
}
void main() {
numberDiamondPattern(3);
}Output:
1
2 2 2
3 3 3 3 3
2 2 2
1This program prints a number diamond pattern where every row consists of its row number repeated 2 * i - 1 times.
Fibonacci Series Pattern
void fibonacciSeriesPattern(int n) {
int a = 0, b = 1;
for (int i = 1; i <= n; i++) {
String row = '';
for (int j = 1; j <= i; j++) {
row += '$a ';
int c = a + b;
a = b;
b = c;
}
print(row);
}
}
void main() {
fibonacciSeriesPattern(4);
}Output:
0
1 1
2 3 5
8 13 21 34This program prints a Fibonacci series pattern of n rows, where row i contains the first i Fibonacci numbers, continuing the sequence across rows.
Character Patterns
Character patterns involve printing various shapes and designs using characters other than asterisks or numbers. Here are 2 examples of character patterns in Dart:
Arrow Pattern
void arrowPattern(int n) {
// Arrowhead: a growing triangle
for (int i = 1; i <= n; i++) {
String row = '';
for (int j = 1; j <= i; j++) {
row += '* ';
}
print(row);
}
// Shaft: a vertical line aligned under the widest row
for (int i = 1; i <= n; i++) {
print(' ' * (2 * n - 2) + '*');
}
}
void main() {
arrowPattern(4);
}Output:
*
* *
* * *
* * * *
*
*
*
*This program prints an arrow pattern: an n-row triangle head followed by an n-row vertical shaft.
Pyramid Pattern
void pyramidPattern(int n) {
for (int i = 1; i <= n; i++) {
String row = '';
for (int j = 1; j <= (n - i); j++) {
row += ' ';
}
for (int k = 1; k <= (2 * i - 1); k++) {
row += '# ';
}
print(row);
}
}
void main() {
pyramidPattern(4);
}Output:
#
# # #
# # # # #
# # # # # # #This program prints a pyramid pattern of n rows using the # character.
Bonus: Palindrome in Dart
Palindromes are another classic interview exercise that pairs well with pattern printing. A palindrome reads the same forwards and backwards. Here is a reusable function in Dart:
bool isPalindrome(String text) {
// Lowercase first, then strip non-alphanumerics — doing it in the
// other order would delete uppercase letters entirely.
final cleaned = text.toLowerCase().replaceAll(RegExp(r'[^a-z0-9]'), '');
final reversed = cleaned.split('').reversed.join();
return cleaned.isNotEmpty && cleaned == reversed;
}
void main() {
print(isPalindrome('racecar')); // true
print(isPalindrome('A man, a plan, a canal: Panama')); // true
print(isPalindrome('flutter')); // false
}The function strips non-alphanumeric characters and lowercases the input, then compares it against its reverse. "racecar" reversed is "racecar", so it returns true.
Conclusion
Pattern printing exercises are a great way to sharpen your loop and string-building skills in Dart. In this article, we covered 10 different pattern printing exercises — star, number, and character patterns — plus a bonus palindrome checker. These exercises are useful not only for their visual appeal but also for building the kind of low-level loop control that interviews and coursework demand.
By mastering these patterns, you can elevate your Dart programming skills and learn to reason precisely about loop boundaries and spacing. With practice, you can even create your own custom patterns. Happy coding!
Related Posts
- Alphabet Rectangle in Dart — With Code, Output & Explanation
- Alphabet Hollow Diamond in Dart — With Code, Output & Explanation
- Alphabet Pyramid in Dart — With Code, Output & Explanation
- Alphabet Rhombus in Dart — With Code, Output & Explanation
References
- Dart language documentation — dart.dev
- Strings and RegExp — dart.dev
- String class — Dart API docs
- Iterable and list operations — dart.dev
FAQ
What are programming patterns in Dart?
In this context, programming patterns are pattern-printing exercises: classic loop practice problems where nested loops print shapes made of stars, numbers, or characters, such as squares, triangles, pyramids, and diamonds. They are not the same as software design patterns like Singleton or Factory, which address code architecture rather than loop control and string-building practice.
How do you print a star triangle pattern in Dart?
Use an outer loop that runs once per row and an inner loop that appends stars to a row string. For a right triangle of n rows, row i gets exactly i stars, so the inner loop runs from 1 to i and prints the accumulated string. Right-aligned versions print leading spaces first to push stars toward the right edge.
How do you print a pyramid pattern in Dart?
A pyramid combines two loops per row: one prints (n minus i) leading spaces, and the second prints (2 times i minus 1) symbols. The leading spaces center each row, while the odd-star count grows the shape symmetrically. Running the row loop from 1 to n produces the top half; reversing it adds the bottom half for a diamond.
How do you check if a string is a palindrome in Dart?
Lowercase the input, strip all non-alphanumeric characters with a regular expression, then compare the cleaned string with its reversed copy using split, reversed, and join. If they match, the string is a palindrome. This approach correctly handles phrases like “A man, a plan, a canal: Panama” because punctuation and spaces are removed before comparison.

Written by Mihir Pipermitwala
Senior Full Stack Consultant & Mobile Engineering Specialist building user-focused applications in healthcare with Flutter, Dart and React Native. I write about what I actually build and test — every tutorial here comes from real project work.
More about Mihir →