0% found this document useful (0 votes)
15 views

Star method using apex

The document provides an Apex code snippet to print a descending star pattern. It explains the structure of the code, including the outer and inner loops, and how to run the code in the Salesforce Developer Console. The output will display rows of stars decreasing from five to one in the debug log.

Uploaded by

MohsinIMP
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Star method using apex

The document provides an Apex code snippet to print a descending star pattern. It explains the structure of the code, including the outer and inner loops, and how to run the code in the Salesforce Developer Console. The output will display rows of stars decreasing from five to one in the debug log.

Uploaded by

MohsinIMP
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Print the following star pattern using Apex code:

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

Answer:

public class StarPattern {


public static void printStarPattern() {
Integer totalRows = 5;

for (Integer i = totalRows; i > 0; i--) {


String row = '';
for (Integer j = 0; j < i; j++) {
row += '*';
}
System.debug(row);
}
}
}
Explanation:

●​ The outer loop (i) controls the number of rows, starting from 5 down to 1.​

●​ The inner loop (j) adds the stars for each row.​

●​ System.debug(row); prints each row to the debug log (Apex uses debug logs instead
of direct console output).

How to Run:

You can run this in the Developer Console in Salesforce:

1.​ Open Developer Console.​

2.​ Go to Debug > Open Execute Anonymous Window.​

3.​ Paste this line and check "Open Log":

StarPattern.printStarPattern();

4. Click "Execute" and view the output in the debug log.

You might also like