Search⌘ K

Solution Review: Pyramid Printing by Using 'for' Loop

Explore how to implement a pyramid printing solution in Java using nested for loops. Understand the role of outer and inner loops to control row and character count, and learn to format output with print statements for clear visual patterns.

We'll cover the following...

Solution #

Java
class HelloWorld {
public static void main( String args[] ) {
int rows = 5;
for (int i = 1; i <= rows; ++i) {
for (int j = 1; j <= i; ++j) {
System.out.print("# ");
}
System.out.println();
}
}
}

Explanation

Here is a breakdown of the above code for your thorough understanding.

  • for(int i = 1; i <= rows; ++i) Outer for loop to iterate till the total number of rows. The outer for ...