SF Learners Hub · Blog · About · Contact

Mastering Star Patterns in Apex: A Visual Delight

By Aman Garg · Published · Salesforce Development

Mastering Star Patterns in Apex: A Visual Delight

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 LinkedIn, Instagram, and Twitter. So you won’t miss out on all future articles.

Related articles

  • Power of Asynchronous Apex: Benefits and Insights — Introduction of Asynchronous Apex Salesforce is renowned for its ability to provide businesses with comprehensive customer relationship management (CRM)…
  • Data Types in Apex — What is a Data Type in Salesforce Apex? In Salesforce Apex, a data type is like a category or a label that tells the computer what kind of information is…
  • Guide to Writing Batchable Classes in Salesforce — Introduction Ever heard of “Batch classes” in Salesforce and felt a bit puzzled? Don’t worry; it’s simpler than you think! Imagine you’re baking cookies. You…
  • Collections in Salesforce — Introduction In the world of customer relationship management (CRM), Salesforce stands tall as a powerhouse, enabling businesses to streamline their…