Mastering Star Patterns in Apex: A Visual Delight

Share This Post

The Art of Star Patterns

Star patterns are aesthetically pleasing geometric arrangements of stars or asterisks. These patterns are not only visually appealing but also fun to create programmatically. In Apex, we can use loops and conditional statements to generate various types of star patterns.

Creating Star Patterns in Apex

Let’s dive into creating star patterns in Apex with some examples:

Example 1: Right Triangle Star Pattern

Integer n = 5; // Number of rows

for (Integer i = 1; i <= n; i++) {
    for (Integer j = 1; j <= i; j++) {
        System.debug('*');
    }
    System.debug('');
}

Output:

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

Example 2: Diamond Star Pattern

Integer n = 5; // Number of rows
Integer mid = (n + 1) / 2;

for (Integer i = 1; i <= mid; i++) {
    for (Integer j = 1; j <= mid - i; j++) {
        System.debug(' ');
    }
    for (Integer j = 1; j <= 2 * i - 1; j++) {
        System.debug('*');
    }
    System.debug('');
}

for (Integer i = mid - 1; i >= 1; i--) {
    for (Integer j = 1; j <= mid - i; j++) {
        System.debug(' ');
    }
    for (Integer j = 1; j <= 2 * i - 1; j++) {
        System.debug('*');
    }
    System.debug('');
}

Output:

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

Example 3: Inverted Right Triangle Star Pattern

Integer n = 5; // Number of rows

for (Integer i = 1; i <= n; i++) {
    for (Integer j = n; j >= i; j--) {
        System.debug('*');
    }
    System.debug('');
}

Output:

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

Example 4: Pyramid Star Pattern

Integer n = 5; // Number of rows

for (Integer i = 1; i <= n; i++) {
    for (Integer j = n; j > i; j--) {
        System.debug(' ');
    }
    for (Integer j = 1; j <= 2 * i - 1; j++) {
        System.debug('*');
    }
    System.debug('');
}

Output:

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

Example 5: Hollow Pyramid Star Pattern

Integer n = 5; // Number of rows

for (Integer i = 1; i <= n; i++) {
    for (Integer j = n; j > i; j--) {
        System.debug(' ');
    }
    for (Integer j = 1; j <= 2 * i - 1; j++) {
        if (j == 1 || j == 2 * i - 1 || i == n) {
            System.debug('*');
        } else {
            System.debug(' ');
        }
    }
    System.debug('');
}

Output:

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

Example 6: Diamond Star Pattern

Integer n = 5; // Number of rows
Integer mid = (n + 1) / 2;

for (Integer i = 1; i <= mid; i++) {
    for (Integer j = 1; j <= mid - i; j++) {
        System.debug(' ');
    }
    for (Integer j = 1; j <= 2 * i - 1; j++) {
        System.debug('*');
    }
    System.debug('');
}

for (Integer i = mid - 1; i >= 1; i--) {
    for (Integer j = 1; j <= mid - i; j++) {
        System.debug(' ');
    }
    for (Integer j = 1; j <= 2 * i - 1; j++) {
        System.debug('*');
    }
    System.debug('');
}

Output:

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

Example 7: Hollow Diamond Star Pattern

Integer n = 5; // Number of rows
Integer mid = (n + 1) / 2;

for (Integer i = 1; i <= mid; i++) {
    for (Integer j = 1; j <= mid - i; j++) {
        System.debug(' ');
    }
    for (Integer j = 1; j <= 2 * i - 1; j++) {
        if (j == 1 || j == 2 * i - 1) {
            System.debug('*');
        } else {
            System.debug(' ');
        }
    }
    System.debug('');
}

for (Integer i = mid - 1; i >= 1; i--) {
    for (Integer j = 1; j <= mid - i; j++) {
        System.debug(' ');
    }
    for (Integer j = 1; j <= 2 * i - 1; j++) {
        if (j == 1 || j == 2 * i - 1) {
            System.debug('*');
        } else {
            System.debug(' ');
        }
    }
    System.debug('');
}

Output:

*
* *
* *
* *
*

Example 8: Half Pyramid Star Pattern (Right-aligned)

Integer n = 5; // Number of rows

for (Integer i = 1; i <= n; i++) {
    for (Integer j = n; j > i; j--) {
        System.debug(' ');
    }
    for (Integer j = 1; j <= i; j++) {
        System.debug('*');
    }
    System.debug('');
}

Output:

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

Example 9: Half Pyramid Star Pattern (Left-aligned)

Integer n = 5; // Number of rows

for (Integer i = 1; i <= n; i++) {
    for (Integer j = 1; j <= i; j++) {
        System.debug('*');
    }
    System.debug('');
}

Output:

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

Example 10: Pyramid Star Pattern (Upside-down)

Integer n = 5; // Number of rows

for (Integer i = n; i >= 1; i--) {
    for (Integer j = 1; j <= n - i; j++) {
        System.debug(' ');
    }
    for (Integer j = 1; j <= 2 * i - 1; j++) {
        System.debug('*');
    }
    System.debug('');
}

Output:

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

Example 11: Hollow Rectangle Star Pattern

Integer rows = 5; // Number of rows
Integer columns = 8; // Number of columns

for (Integer i = 1; i <= rows; i++) {
    for (Integer j = 1; j <= columns; j++) {
        if (i == 1 || i == rows || j == 1 || j == columns) {
            System.debug('*');
        } else {
            System.debug(' ');
        }
    }
    System.debug('');
}

Output:

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

Example 12: Diagonal Line of Stars

Integer n = 5; // Number of rows

for (Integer i = 1; i <= n; i++) {
    for (Integer j = 1; j <= n; j++) {
        if (i == j) {
            System.debug('*');
        } else {
            System.debug(' ');
        }
    }
    System.debug('');
}

Output:

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

Example 13: Butterfly Star Pattern

Integer n = 5; // Number of rows

for (Integer i = 1; i <= n; i++) {
    for (Integer j = 1; j <= i; j++) {
        System.debug('*');
    }
    for (Integer j = 1; j <= 2 * (n - i); j++) {
        System.debug(' ');
    }
    for (Integer j = 1; j <= i; j++) {
        System.debug('*');
    }
    System.debug('');
}

for (Integer i = n; i >= 1; i--) {
    for (Integer j = 1; j <= i; j++) {
        System.debug('*');
    }
    for (Integer j = 1; j <= 2 * (n - i); j++) {
        System.debug(' ');
    }
    for (Integer j = 1; j <= i; j++) {
        System.debug('*');
    }
    System.debug('');
}

Output:

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

Example 14: Cross Star Pattern

Integer n = 5; // Number of rows

for (Integer i = 1; i <= n; i++) {
    for (Integer j = 1; j <= n; j++) {
        if (j == i || j == n - i + 1) {
            System.debug('*');
        } else {
            System.debug(' ');
        }
    }
    System.debug('');
}

Output:

* *
* *
*
* *
* *

Example 15: Hourglass Star Pattern

Integer n = 5; // Number of rows

for (Integer i = 1; i <= n; i++) {
    for (Integer j = 1; j <= i; j++) {
        System.debug(' ');
    }
    for (Integer j = 1; j <= 2 * (n - i) + 1; j++) {
        System.debug('*');
    }
    System.debug('');
}

for (Integer i = n - 1; i >= 1; i--) {
    for (Integer j = 1; j <= i; j++) {
        System.debug(' ');
    }
    for (Integer j = 1; j <= 2 * (n - i) + 1; j++) {
        System.debug('*');
    }
    System.debug('');
}

Output:

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

Example 16: Zigzag Star Pattern

Integer n = 5; // Number of rows

for (Integer i = 1; i <= n; i++) {
    for (Integer j = 1; j <= i; j++) {
        System.debug('*');
    }
    System.debug('');
}

for (Integer i = n - 1; i >= 1; i--) {
    for (Integer j = 1; j <= i; j++) {
        System.debug('*');
    }
    System.debug('');
}

Output:

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

Example 17: Zigzag Star Pattern (Alternate Rows)

Integer n = 5; // Number of rows

for (Integer i = 1; i <= n; i++) {
    if (i % 2 == 0) {
        System.debug(' * * * * *');
    } else {
        for (Integer j = 1; j <= n; j++) {
            System.debug('*');
        }
    }
    System.debug('');
}

Output:

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

Conclusion

Creating star patterns in Apex is not only a creative exercise but also a testament to the versatility of the Salesforce platform. By leveraging the power of loops and conditional statements, you can bring visual delight to your applications and enhance user experiences. So, why not add a touch of magic to your Salesforce development with star patterns? Start exploring, creating, and innovating with Apex today!

I hope you like this blog and if you want any help let me know in the comment section.

Stay tuned, there is way more to come! Follow me on LinkedInInstagram, and Twitter. So you won’t miss out on all future articles.

15 Responses

  1. Hiya, I am really glad I have found this info. Today bloggers publish only about gossips and internet and this is actually frustrating. A good blog with exciting content, this is what I need. Thanks for keeping this web site, I will be visiting it. Do you do newsletters? Can’t find it.

  2. Hello very nice website!! Man .. Excellent .. Superb .. I will bookmark your blog and take the feeds also…I am happy to search out a lot of helpful info here in the submit, we’d like work out more techniques on this regard, thanks for sharing.

  3. I enjoy what you guys are up too. Such clever work and coverage! Keep up the wonderful works guys I’ve you guys to blogroll.

  4. Hey there! This is kind of off topic but I need some advice from an established blog. Is it hard to set up your own blog? I’m not very techincal but I can figure things out pretty quick. I’m thinking about making my own but I’m not sure where to begin. Do you have any ideas or suggestions? Thanks

  5. I happen to be writing to let you understand of the brilliant experience my daughter gained checking your site. She mastered plenty of details, with the inclusion of what it is like to have an excellent giving style to have other folks quite simply know precisely selected impossible issues. You undoubtedly surpassed my desires. Many thanks for supplying such interesting, trustworthy, educational and even easy guidance on your topic to Julie.

  6. This website can be a walk-through for all the data you wanted about this and didn’t know who to ask. Glimpse here, and also you’ll definitely discover it.

  7. I have learn several just right stuff here. Definitely price bookmarking for revisiting. I wonder how much effort you place to create this sort of wonderful informative website.

  8. It’s appropriate time to make a few plans for the longer term and it’s time to be happy. I have read this submit and if I may I desire to recommend you some interesting issues or suggestions. Maybe you can write subsequent articles referring to this article. I desire to learn more issues about it!

  9. Thank you for the sensible critique. Me & my neighbor were just preparing to do a little research on this. We got a grab a book from our local library but I think I learned more from this post. I am very glad to see such great information being shared freely out there.

  10. It’s really a great and helpful piece of info. I am glad that you shared this useful info with us. Please keep us informed like this. Thanks for sharing.

Leave a Reply to https://odessaforum.biz.ua/Cancel reply

Subscribe To Our Newsletter

Get updates and learn from the best

More To Explore

Discover more from SF Learners Hub

Subscribe now to keep reading and get access to the full archive.

Continue reading