Print a Staircase Pattern in Java – Best 3 Ways

In this post, we will learn how to print a Staircase Pattern in Java. We will learn some simple Staircase patterns as they can be presented in multiple shapes.

What are Staircase patterns?

As a staircase is the combination of multiple stairs and as it is always in an upside manner, similarly through this tutorial, we will learn how to do the complete program to represent this type of Staircase Pattern in Java.

Examples:

What are Staircase patterns

Also Read: Java Program To Print Vowels In A String – 2 Simple Programs

Logic Behind First Program(How to Think Before Do This Program?)

Let’s learn the logic for the left-side staircase pattern.

As we have to display a left staircase pattern. So first of all, we need to print any string(like * or #) in a manner so that it will be displayed like a Staircase Pattern.

Suppose we will print “#” for the staircase pattern. So to print this “#” in the staircase structure, we need to print one “#” in the first line, two “#” in the second line, three “#” in the third line, and so on. It will continue until the user given no of lines will be completed.

So, the first question is how to print this “#” in the above structure?

If we will use for loop, then we can iterate to display multiple lines in Java and as we have to display multiple “#” from the second line, So we need to use 2 different for loops for both rows and columns.

Yes, we will use one for loop inside another for loop which is nested for loop and we will use print statement inside the second for loop to display the “#”. When one line will be completed, we will start the next “#” from the next line onwards.

That’s it! by using this logic, we can print a Staircase Pattern in Java. Let’s see the complete program now.

Program-1: How to Print a Staircase Pattern in Java? (Left Staircase Pattern)

/** * How to Print a Staircase Pattern in Java?(Left Staircase Pattern) */ package cf.java.pattern; import java.util.Scanner; public class LeftStaircasePattern { public static void main(String[] args) { // Declare all varibales int noOfLines; // Use Scanner to accept no of lines pattern Scanner sc = new Scanner(System.in); System.out.print("Enter no of lines for pattern you want: "); noOfLines = sc.nextInt(); // Display left staircase pattern System.out.println("Left Staircase Pattern:"); for(int row = 0; row < noOfLines; row++) { for(int col = 0; col < noOfLines; col++) { if(row >= col) System.out.print("#"); } // Display in a new line System.out.println(); } } }
Code language: PHP (php)

Output:

Enter no of lines for pattern you want: 5 Left Staircase Pattern: # ## ### #### #####
Code language: PHP (php)

Also Read: How to convert List to Byte Array in Java? – Easy way

Logic Behind Second Program

Let’s learn the logic for the right-side staircase pattern.

As we have to display a right staircase pattern. So first of all, we need to print any string(like * or #) in a manner so that it will be displayed like a Right Staircase Pattern.

Suppose we will print “#” for the staircase pattern. So to print this “#” in the staircase structure, we need to print both blank space and also “#” simultaneously.

So, the first question is how to print this “#” in the above structure?

If we will use for loop, then we can iterate to display multiple lines in Java and as we have to display both space and also “#” from the first line, So we need to use 2 different for loops for both Space and # also.

Yes, we will use one for loop inside another for loop which is nested for loop, and an inner for loop, we will use the if-else statement( if(col < noOfLines – row -1) ) to display both blank space and also “#” simultaneously.

That’s it! by using this logic, we can print the right Staircase Pattern in Java. Let’s see the complete program now.

Program-2: How to Print a Staircase Pattern in Java? (Right Staircase Pattern)

/** * How to Print a Staircase Pattern in Java?(Right Staircase Pattern) */ package cf.java.pattern; import java.util.Scanner; public class RightStaircasePattern { public static void main(String[] args) { // Declare all varibales int noOfLines; // Use Scanner to accept no of lines pattern Scanner sc = new Scanner(System.in); System.out.print("Enter no of lines for pattern you want: "); noOfLines = sc.nextInt(); // Display left staircase pattern System.out.println("Right Staircase Pattern:"); for (int row = 0; row < noOfLines; row++) { for (int col = 0; col < noOfLines; col++) { if (col < noOfLines - row - 1) System.out.print(" "); else System.out.print("#"); } // Display in a new line System.out.println(); } } }
Code language: PHP (php)

Output:

Enter no of lines for pattern you want: 5 Right Staircase Pattern: # ## ### #### #####
Code language: PHP (php)

Also Read: Java Program To Remove Duplicate Words In A String – 3 Ways

Logic Behind Third Program

Let’s learn the logic for the real side staircase pattern.

Suppose we will print “#” for the real staircase pattern. So here we have used if else statement for checking row value is even or not. If it will be even, we will increase the value of k by 1 otherwise we will assign i value to k. When it will be even, we will print same no of “#” two times.

Let’s see the complete program now.

Program-3: How to Print a Staircase Pattern in Java? (Real Staircase Pattern)

/** * How to Print a Staircase Pattern in Java?(Real Staircase Pattern) */ package cf.java.pattern; import java.util.Scanner; public class RealStaircasePattern { public static void main(String[] args) { // Declare all varibales int noOfLines, k; // Use Scanner to accept no of lines pattern Scanner sc = new Scanner(System.in); System.out.print("Enter no of lines for pattern you want: "); noOfLines = sc.nextInt(); // Display left staircase pattern System.out.println("Real Staircase Pattern:"); for (int i = 0; i <= noOfLines; i++) { // Check whether value of i is even or not if (i % 2 != 0) k = i + 1; else k = i; for (int j = 0; j < k; j++) System.out.print("# "); System.out.println(); } } }
Code language: PHP (php)

Output:

Enter no of lines for pattern you want: 8 Real Staircase Pattern: # # # # # # # # # # # # # # # # # # # # # # # #
Code language: PHP (php)

Other Queries Those We Have Covered

  • Java Program for Stair Case Patterns.
  • How to solve staircase problem in Java?
  • Staircase HackerRank Solution in java.
  • How to print simple star pattern in Java?

FAQs

What are Staircase patterns?

Staircase pattern is the pattern of representation of String in such a manner so that it will be looks like a Staircase structure.

What is the logic for Staircase pattern?

We can use the below simple logic for Staircase pattern:
for(int row = 0; row < noOfLines; row++) {
for(int col = 0; col < noOfLines; col++) {
if(row >= col) System.out.print(“#”);
}
System.out.println();

How to print right aligned staircase pattern?

We can use 2 different for loops for both Space and also #.
We will use one for loop inside another for loop which is nested for loop, and an inner for loop, we will use the if-else statement( if(col < noOfLines – row -1) ) to display both blank space and also “#” simultaneously.

Leave a Comment