Creating a Symmetrical Star Pattern in Dart

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:

*
**
***
****
*****
******
*******
********
*********


This content originally appeared on DEV Community and was authored by Muhammadh Ahzem

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:

  1. 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.
  2. 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! 🌟


This content originally appeared on DEV Community and was authored by Muhammadh Ahzem


Print Share Comment Cite Upload Translate Updates
APA

Muhammadh Ahzem | Sciencx (2024-07-09T01:49:16+00:00) Creating a Symmetrical Star Pattern in Dart. Retrieved from https://www.scien.cx/2024/07/09/creating-a-symmetrical-star-pattern-in-dart/

MLA
" » Creating a Symmetrical Star Pattern in Dart." Muhammadh Ahzem | Sciencx - Tuesday July 9, 2024, https://www.scien.cx/2024/07/09/creating-a-symmetrical-star-pattern-in-dart/
HARVARD
Muhammadh Ahzem | Sciencx Tuesday July 9, 2024 » Creating a Symmetrical Star Pattern in Dart., viewed ,<https://www.scien.cx/2024/07/09/creating-a-symmetrical-star-pattern-in-dart/>
VANCOUVER
Muhammadh Ahzem | Sciencx - » Creating a Symmetrical Star Pattern in Dart. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/07/09/creating-a-symmetrical-star-pattern-in-dart/
CHICAGO
" » Creating a Symmetrical Star Pattern in Dart." Muhammadh Ahzem | Sciencx - Accessed . https://www.scien.cx/2024/07/09/creating-a-symmetrical-star-pattern-in-dart/
IEEE
" » Creating a Symmetrical Star Pattern in Dart." Muhammadh Ahzem | Sciencx [Online]. Available: https://www.scien.cx/2024/07/09/creating-a-symmetrical-star-pattern-in-dart/. [Accessed: ]
rf:citation
» Creating a Symmetrical Star Pattern in Dart | Muhammadh Ahzem | Sciencx | https://www.scien.cx/2024/07/09/creating-a-symmetrical-star-pattern-in-dart/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.