To create a pyramid of characters in Java, you can use loops to control the pattern and structure of the pyramid. Here’s an example code that demonstrates how to create a pyramid of characters:
<span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">Pyramid</span> {
<span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> {
<span class="hljs-type">int</span> <span class="hljs-variable">rows</span> <span class="hljs-operator">=</span> <span class="hljs-number">5</span>; <span class="hljs-comment">// Number of rows in the pyramid</span>
<span class="hljs-type">char</span> <span class="hljs-variable">symbol</span> <span class="hljs-operator">=</span> <span class="hljs-string">'*'</span>; <span class="hljs-comment">// Character to be displayed</span>
<span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">i</span> <span class="hljs-operator">=</span> <span class="hljs-number">1</span>; i <= rows; i++) {
<span class="hljs-comment">// Print spaces before the characters</span>
<span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">j</span> <span class="hljs-operator">=</span> <span class="hljs-number">1</span>; j <= rows - i; j++) {
System.out.print(<span class="hljs-string">" "</span>);
}
<span class="hljs-comment">// Print the characters</span>
<span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">k</span> <span class="hljs-operator">=</span> <span class="hljs-number">1</span>; k <= (<span class="hljs-number">2</span> * i - <span class="hljs-number">1</span>); k++) {
System.out.print(symbol);
}
<span class="hljs-comment">// Move to the next line</span>
System.out.println();
}
}
}
In this example, the rows variable determines the number of rows in the pyramid, and the symbol variable holds the character that will be displayed. You can modify these variables to create pyramids of different sizes or with different characters.
The first for loop is responsible for printing spaces before the characters. It starts from the first row and continues until the last row. The number of spaces decreases by one in each iteration, which creates the pyramid shape.
The second for loop prints the characters. The number of characters in each row is calculated using the formula 2 * i - 1, where i represents the current row number. This formula ensures that the number of characters in each row increases by two with each iteration.
Finally, the code moves to the next line using System.out.println() after printing each row to create the pyramid structure.
When you run this program, it will output the following pyramid:
<span class="hljs-bullet"> *</span>
<span class="hljs-strong">***
</span>
<span class="hljs-strong"> **</span><span class="hljs-strong">***
</span>
<span class="hljs-strong"> **</span><span class="hljs-strong">****</span>*
<span class="hljs-strong">****</span><span class="hljs-strong">****</span>*
Feel free to adjust the rows and symbol variables to create pyramids of different sizes or with different characters as per your requirement.
