Step 1:Â First, we will declare a number of variables of integer data type according to the pattern of the pyramid. For example i, j,k, and rows. Variable i indicates the number of rows, variable j and k indicate the number of columns, and rows is used to take input from the user.
Step 2:Â Then, We will take the value from the user to print the number of rows to print using Scanner and store it in the rows variable.
Step 3: We will write more than two ‘for’ loops according to the pattern of the pyramid. The outer ‘for’ loop iterates the number of rows, while other inner loops iterate the number of columns as per requirement. The symbol, character, and numbers are then printed based on the number of columns. We will write a print statement in the second loop for printing symbols or characters or numbers.
A pattern program which has a pyramid shape is called the pyramid program in Java. We will discuss many such pattern programs. In most of the patterns given below, the first for loop is used to traverse the rows while the second for loop is used to traverse the columns of those rows
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.
