Open In App

Java Program For Printing 180 Degree Rotation of Simple Half Left Pyramid

Last Updated : 22 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

We can utilize ‘for’ and ‘while’ loops to print a 180-degree rotation of a simple half-left pyramid in Java as follows:

Example of 180-degree Rotation of Simple Half-Left Pyramid

Rows = 5

Output :

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

Methods For Printing 180-Degree Rotation of Simple Half-Left Pyramid

There are certain methods to Print the 180-rotated Simple Half-Left Pyramid mentioned below:

  • Using a ‘for’ loop
  • Using a ‘while’ loop

1. Using a ‘for’ loop

In this approach two for loops are utilized where, the first for loop identifies the number of rows while the second for loop identifies a number of columns. Here the values will be altered according to the first for loop. If j is greater than i then it will print the output otherwise it will print the space.

Below is the implementation of the above method:

Java




//Java program for above approach
public class PyramidRotation {
    public static void main(String[] args) {
        int rows = 5;
  
        // The outer loop is used to identify the number of rows.
        for (int i = 0; i < rows; i++) {
  
            // The inner loop is used to print spaces before the asterisks.
            for (int j = 0; j < rows - i - 1; j++) {
                System.out.print("  ");
            }
  
            // The second inner loop is used to print asterisks.
            for (int k = 0; k <= i; k++) {
                System.out.print("* ");
            }
  
            System.out.println(); // Move to the next line after each row.
        }
    }
}


Output

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

2. Using a ‘while’ loop

Explanation: The while loop checks for the condition until the condition becomes false. If the condition is true then it enters into the loop and executes the statements

Below is the implementation of the above method:

Java




//Java Program for above approach
public class PyramidRotation {
    public static void main(String[] args) {
        int i = 0, j = 0, sp = 0;
        int rows = 5;
  
        // The outer while loop continues until the condition (i < rows) is false.
        while (i < rows) {
  
            // The inner while loop is used to print spaces before the asterisks.
            while (sp < (rows - i - 1)) {
                System.out.print("  "); // Printing two spaces.
                sp++;
            }
  
            // Reset sp to 0 for the next row.
            sp = 0;
  
            // This loop prints the asterisks and spaces pattern.
            while (j <= i) {
                System.out.print("* "); // Printing an asterisk followed by a space.
                j++;
            }
  
            // Reset j to 0 for the next row.
            j = 0;
            i++;
            System.out.println(); // Move to the next line after each row.
        }
    }
}


Output

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

Complexity of the above method

Time complexity: O(n2) for given input of ‘n’ rows
Auxiliary space: O(1)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads