dev-resources.site
for different kinds of informations.
Creating a Symmetrical Star Pattern in Dart
Published at
7/9/2024
Categories
Author
Muhammadh Ahzem
Categories
1 categories in total
open
Hey everyone!
I recently worked on a fun coding challenge to generate a symmetrical star pattern using Dart. Here's the pattern I wanted to create:
*
**
***
****
*****
******
*******
********
*********
**********
*********
********
*******
******
*****
****
***
**
*
After some trial and error, I came up with the following solution:
void main() {
int n = 10; // Height of the pattern
// Print the upper half of the pattern
for (int i = 1; i <= n; i++) {
print(" " * (i - 1) + "*" * i);
}
// Print the lower half of the pattern
for (int i = n - 1; i > 0; i--) {
print(" " * (n - i) + "*" * i);
}
}
How It Works:
- The first loop generates the upper half of the pattern, starting with 1 star and incrementing up to 10 stars, each line indented with increasing spaces.
- The second loop creates the lower half of the pattern, starting from 9 stars down to 1 star, maintaining the symmetry by increasing the leading spaces.
Key Takeaways:
- This exercise helped me reinforce my understanding of nested loops and string manipulation in Dart.
- By carefully controlling the number of spaces and stars in each iteration, we can create visually appealing patterns.
Feel free to try it out and let me know if you have any suggestions for improvement!
Happy coding! 🌟
Articles
10 articles in total
Getting Started with AWS EC2: A Complete Guide for Beginners
read article
My Hacktoberfest 2024 Journey
read article
My Hacktoberfest 2024 Journey
read article
Creating a Symmetrical Star Pattern in Dart
currently reading
First Contributions: learn how to contribute to open source projects
read article
How Microsoft Learn can help you grow your skills
read article
Placeholder Contributor
read article
Placeholder Contributor
read article
Enhancing Security and Simplicity with Appwrite Authentication
read article
Hacktoberfest23 Pledge
read article
Featured ones: