Java program to print left-aligned and right-aligned staircase patterns

How to print a staircase pattern in Java:

In this post, we will learn to print a staircase pattern in Java. The staircase pattern can be a right-aligned staircase, left-aligned staircase or real staircase pattern. For each pattern, the algorithms will be different. Before we start writing code, I will explain to you the algorithms for each.

Pattern 1: Algorithm of Left aligned staircase:

A left-aligned staircase looks as below:

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

It is a left-aligned staircase of height 5.

  • We can use two loops to print this pattern. One outer loop and another inner loop.
  • The outer loop will run for height times. Let’s say it runs from i = 1 to i = height. It will point to each row of the staircase pattern on each iteration.
  • The inner loop will run for i times. In this inner loop, we will print the stars. It will print 1 star for the first row, 2 stars for the second one etc.
  • At the end of each iteration of the outer loop, we will have to print one new line character to start the next print from the next line.

Java program of left-aligned staircase:

The below Java program prints a left-aligned staircase with user input height:

import java.util.Scanner;

class Main {

    public static void main(String[] args) {
        int height;
        Scanner scanner = new Scanner(System.in);

        System.out.println("Enter the height of the staircase pattern: ");
        height = scanner.nextInt();

        for (int i = 1; i <= height; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print("* ");
            }
            System.out.println();
        }
    }
}

Here,

  • height is the height of the pattern.
  • The Scanner object is used to read the user inputs.
  • The for loop is used to print the pattern. The outer loop runs for height times and the inner loop runs for i times.
  • Inside the inner loop, we are printing the stars.
  • At the end of each iteration of the loop, it prints a new line.

If you run the above program, it will give outputs like below:

Enter the height of the staircase pattern: 
5
* 
* * 
* * * 
* * * * 
* * * * * 

Enter the height of the staircase pattern: 
6
* 
* * 
* * * 
* * * * 
* * * * * 
* * * * * * 

Pattern 2: Algorithm of right-aligned staircase:

The right-aligned staircase is opposite of the left-aligned staircase. A right-aligned staircase of height 6 is:

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

We are using blank spaces and stars to print this pattern. To understand how it works, let me replace all blank spaces with #:

# # # # # * 
# # # # * * 
# # # * * * 
# # * * * * 
# * * * * * 
* * * * * * 
  • We can use two loops for this pattern as well. The outer loop will point to each row of the pattern and the inner loop will print the blank spaces and stars of the pattern.
  • The outer loop will run from i = 1 to i = height.
  • On each iteration, two inner loops will run. The first inner loop will run from j = 1 to j = height - i to print the blank spaces and the second inner loop will run from j = 1 to j = i to print the stars.
  • At the end of each iteration of the outer loop, it will print one new line.

Let’s write down the program:

Java program to print a right-aligned staircase pattern:

We can use the below program to print a right-aligned staircase pattern:

import java.util.Scanner;

class Main {

    public static void main(String[] args) {
        int height;
        Scanner scanner = new Scanner(System.in);

        System.out.println("Enter the height of the staircase pattern: ");
        height = scanner.nextInt();

        for (int i = 1; i <= height; i++) {
            for(int j = 1; j <= height - i; j++){
                System.out.print("  ");
            }
            for (int j = 1; j <= i; j++) {
                System.out.print("* ");
            }
            System.out.println();
        }
    }
}

It is similar to the previous example. It uses two inner for loops to print the blank spaces and stars of the pattern.

Output:

Enter the height of the staircase pattern: 
7
            * 
          * * 
        * * * 
      * * * * 
    * * * * * 
  * * * * * * 
* * * * * * * 

Example 3: Real staircase pattern:

A real staircase pattern looks as below:

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

The number of times stars are printed on each row is different for this pattern. This count increases by 2 after two lines. We can use a separate count variable to hold the number of times we need to print the stars.

  • The program will use two for-loops similar to the previous examples.
  • It will initialize a variable count as 0 to define the number of stars to print on each step.
  • The outer loop will run from i = 0 to i = height - 1, where height is the height of the pattern.
  • If the value of i is even, it will increase the count variable by 2.
  • The inner loop will run for count number of times to print the stars.

Below is the complete program:

import java.util.Scanner;

class Main {

    public static void main(String[] args) {
        int height;
        Scanner scanner = new Scanner(System.in);

        System.out.println("Enter the height of the staircase pattern: ");
        height = scanner.nextInt();

        int count = 0;

        for (int i = 0; i < height; i++) {
            if (i % 2 == 0) {
                count += 2;
            }
            for (int j = 0; j < count; j++) {
                System.out.print("* ");
            }
            System.out.println();
        }
    }
}

Output:

Enter the height of the staircase pattern: 
8
* * 
* * 
* * * * 
* * * * 
* * * * * * 
* * * * * * 
* * * * * * * * 
* * * * * * * * 

Enter the height of the staircase pattern: 
10
* * 
* * 
* * * * 
* * * * 
* * * * * * 
* * * * * * 
* * * * * * * * 
* * * * * * * * 
* * * * * * * * * * 
* * * * * * * * * * 

You might also like: