The Art of Programming Patterns in Dart: Star, Number & Character

By Mihir Pipermitwala · · 10 min read

⚡ 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

Copy
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:

Copy
* * * *
* * * *
* * * *
* * * *

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

Copy
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:

Copy
*
* *
* * *
* * * *

This program prints a right triangle pattern of n rows. Row i has exactly i stars.

Left Triangle Pattern (Right-Aligned)

Copy
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:

Copy
      *
    * *
  * * *
* * * *

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

Copy
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:

Copy
   *
  * * *
 * * * * *
* * * * * * *
 * * * * *
  * * *
   *

This program prints a diamond pattern with a widest row of n stars. Each row i has 2 * i - 1 stars.

Hourglass Pattern

Copy
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:

Copy
* * * * * * *
 * * * * *
  * * *
   *
  * * *
 * * * *
* * * * * * *

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

Copy
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:

Copy
   1
  1 2
 1 2 3
1 2 3 4

This program prints a number pyramid pattern of n rows, where row i contains the numbers 1 through i.

Number Diamond Pattern

Copy
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:

Copy
  1
 2 2 2
3 3 3 3 3
 2 2 2
  1

This program prints a number diamond pattern where every row consists of its row number repeated 2 * i - 1 times.

Fibonacci Series Pattern

Copy
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:

Copy
0
1 1
2 3 5
8 13 21 34

This 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

Copy
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:

Copy
*
* *
* * *
* * * *
      *
      *
      *
      *

This program prints an arrow pattern: an n-row triangle head followed by an n-row vertical shaft.

Pyramid Pattern

Copy
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:

Copy
   #
  # # #
 # # # # #
# # # # # # #

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:

Copy
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!

References

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.

Mihir Pipermitwala

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 →
Scroll down to load comments...
Related Tutorials
Essential Git Skills: How to Delete Remote Branches

Essential Git Skills: How to Delete Remote Branches

Keep your repository clean and organized. This guide walks you through how to delete both local and remote branches in Git, step by step.

BITBUCKETDELETE LOCAL BRANCHDELETE REMOTE BRANCHGITGIT BRANCH CLEANUP AUTOMATION

June 12, 2024

Show an Offline Message for No Internet in Flutter

Show an Offline Message for No Internet in Flutter

You have to use the 'Connectivity Flutter Package' to achieve this feature on your App. This package helps to know whether your device is online or offline.

CONNECTIVITY PLUSDEPENDENCIESFLUTTERFLUTTER DEVELOPMENTFLUTTER PACKAGES

April 09, 2024

Mastering TabBar & TabBarView in Flutter: A Complete Guide

Mastering TabBar & TabBarView in Flutter: A Complete Guide

Implement TabBar and TabBarView in Flutter step by step — customize tab indicators, enable scrollable tabs, and change tabs programmatically.

CODINGDEFAULT TAB CONTROLLERFLUTTERLEARN TO CODETAB CONTROLLER

July 26, 2023

File Manipulation in Flutter: Best Practices & Examples

File Manipulation in Flutter: Best Practices & Examples

Master Flutter file manipulation — permission management, directory handling, and practical read-write scenarios to elevate your app's file handling.

CODINGFLUTTERFLUTTER DEVELOPMENTFLUTTER FILE OPERATIONFLUTTER PATH PROVIDER

June 30, 2023

Related Recommended Services
Visual Studio Code for the Web

Visual Studio Code for the Web

Build with Visual Studio Code, anywhere, anytime, in your browser.

IDEVISUAL STUDIOVISUAL STUDIO CODEWEB
Renovate | Automated Dependency Updates

Renovate | Automated Dependency Updates

Renovate Bot keeps source code dependencies up-to-date using automated Pull Requests.

AUTOMATED DEPENDENCY UPDATESBUNDLERCOMPOSERGITHUBGO MODULES
Best XML Formatter and XML Beautifier

Best XML Formatter and XML Beautifier

Online XML Formatter will format xml data, helps to validate, and works as XML Converter. Save and Share XML.

XMLXML BEAUTIFIERXML CONVERTERXML FORMATXML FORMATTER
Kubecost | Kubernetes cost monitoring and management

Kubecost | Kubernetes cost monitoring and management

Kubecost started in early 2019 as an open-source tool to give developers visibility into Kubernetes spend. We maintain a deep commitment to building and supporting dedicated solutions for the open source community.

CLOUDKUBECOSTKUBERNETESOPEN SOURCESELF HOSTED
Related Recommended Stories
How GitHub reduced testing time for iOS apps with new runner features

How GitHub reduced testing time for iOS apps with new runner features

Learn how GitHub used macOS and Apple Silicon runners for GitHub Actions to build, test, and deploy our iOS app faster.

IOSGITHUBTESTINGRUNNER
5 ways to transform your workflow using GitHub Copilot and MCP

5 ways to transform your workflow using GitHub Copilot and MCP

Learn how to streamline your development workflow with five different MCP use cases.

AGENT MODECODING AGENTCOPILOTFIGMAGITHUB
One weird trick for powerful Git aliases

One weird trick for powerful Git aliases

Advanced Git Aliases

ALIASALIAS TEMPLATEATLASSIANBITBUCKETGIT
Awesome Python

Awesome Python

An opinionated list of awesome Python frameworks, libraries, software and resources

AWESOMEAWESOME PYTHONCOLLECTIONSGITHUBPYTHON
Related Recommended Tools
Find out what websites are built with - Wappalyzer

Find out what websites are built with - Wappalyzer

Find out the technology stack of any website. Create lists of websites and contacts by the technologies they use.

ADD ONSANALYTICSAPP STOREAPPLEBOOKING
Sourcetree | Free Git GUI for Mac and Windows

Sourcetree | Free Git GUI for Mac and Windows

A Git GUI that offers a visual representation of your repositories. Sourcetree is a free Git client for Windows and Mac.

GITGITHUBGITLABATLASSIANBITBUCKET
Related Recommended Videos