Last Updated on December 27, 2022 by Prepbytes
The Java pattern program will enhance logic, looping ideas, and coding abilities. It is frequently asked in Java interviews to test the programmer’s reasoning. A Java pattern program can be printed in a variety of styles. We need to have a thorough understanding of Java loops like the for loop and do-while loop in order to grasp pattern programming. We will discover how to print a pattern in Java in this part.
We have classified the Java pattern program into three categories:
- Star Pattern
- Number Pattern
- Character Pattern
When we implement the logic, the inner loop will print x 1 time in the first row, then two times in the second row, and so on.
Tips: if we want to print the inverted triangle, the only change required is to change the first for loop such that we iterate from i=rows – 1 to i=0, and we are good to go.
Printing Half Pyramid Using Star (*)
To print a half pyramid/triangle using , we will replace the x with , and we are good to go.
Java Code for Pyramid Pattern
class RightTrianglePattern { public static void main(String args[]) { //i for rows and j for columns //row denotes the number of rows you want to print int i, j, row=6; //outer loop for rows for(i=0; i<row; i++) { //inner loop for columns for(j=0; j<=i; j++) { //prints stars System.out.print("* "); } //throws the cursor in a new line after printing each line System.out.println(); } } }
Output
*
* *
* * *
* * * *
* * * * *
Printing Half Pyramid Using Numbers
Again to print a pyramid of numbers, we need to replace x with i+1, where i+1 denotes the row number. Here it is i+1 because we are starting ii from 0.
Java Code for Printing Half Pyramid
import java.util.*; class Pattern { public static void main(String[] args) { int i, j, rows; Scanner sc = new Scanner(System.in); System.out.println("Enter the number of rows you want to print: "); rows = sc.nextInt(); for (i = 1; i <= rows; i++) { for (j = 1; j <= i; j++) { System.out.print(i+" "); } System.out.println(); } } }
Output
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
Printing Half Pyramid Using Alphabets
Similarly, we will replace the x with the desired alphabets to print a pyramid of alphabets.
Java Code for Pyramid Using Alphabets
class RightAlphabaticPattern { public static void main(String[] args) { int alphabet = 65; //ASCII value of capital A is 65 //inner loop for rows for (int i = 0; i <= 8; i++) { //outer loop for columns for (int j = 0; j <= i; j++) { //adds the value of j in the ASCII value of A and prints the corresponding alphabet System.out.print((char) (alphabet + j) + " "); } System.out.println(); } } }
Output
A
A B
A B C
A B C D
A B C D E
A B C D E F
Printing Rectangle Pattern
Coding a rectangle pattern program in Java is similar to printing a matrix of dimensions n×m.
The logic is as follows:
- Take the input of rows and columns (dimensions) of the rectangle. Let them be rows and cols.
- Iterate from i=0 to i=rows and do the following :
- Iterate from j=0 to j=cols and print x + space each time i.ei.e System.out.print(X);
- Terminate the current line, i.e. System.out.println();
Java Code for Rectangle Pattern
import java.util.Scanner; class RectangleStar1 { private static Scanner sc; public static void main(String[] args) { int rows, columns, i, j; sc = new Scanner(System.in); System.out.println(" Please Enter Number of Rows : "); rows = sc.nextInt(); System.out.println(" Please Enter Number of Columns : "); columns = sc.nextInt(); for(i = 1; i <= rows; i++) { for(j = 1; j <= columns; j++) { System.out.print("* "); } System.out.print("\n"); } } }
Output
* * * * * *
* * * * * *
* * * * * *
* * * * * *
Printing Hollow Rectangle Pattern
The process to code the hollow rectangle pattern program in C is almost the same as printing a normal rectangle with the only difference that while printing a hollow rectangle, we need to take care of printing empty spaces for non-border cells.
The logic is as follows :
- Take the input of rows and columns (dimensions) of the rectangle. Let them be rows and cols.
- Iterate from i=0 to i=rows and do the following :
- Iterate from j=0 to j=cols and do the following :
- If the current is the border cell of a rectangle i.e.i.e. i = 0 OR i = rows – 1 OR j = 0 OR j = cols – 1 print * + space.
- Otherwise, print blank space.
- Iterate from j=0 to j=cols and do the following :
Java Code for Hollow Rectangle Pattern
import java.io.*; class Prepbuddy { static void print_rectangle(int n, int m) { int i, j; for (i = 1; i <= n; i++) { for (j = 1; j <= m; j++) { if (i == 1 || i == n || j == 1 || j == m) System.out.print("*"); else System.out.print(" "); } System.out.println(); } } public static void main(String args[]) { int rows = 6, columns = 20; print_rectangle(rows, columns); } }
Output
********************
* *
* *
* *
* *
********************
Floyd’s Triangle
In Floyd’s triangle, we need to print numbers, but it is also required to increase the number by 1 each time we print it. Floyd’s Triangle is similar to the half triangle using numbers, with the only difference being that in Floyd’s triangle, we need to maintain a counter which will be incremented by one each time it is printed.
The logic is as follows :
- Take input from the user on the number of rows in the triangle. Let it be rows.
- Define a variable, say counter, and initialize its value to 0.
- Iterate for i=0 to i=rows-1 and do :
- Iterate for j=0 to j=rows and do the following :
- Print count + space each time i.e. System.out.print(count);
- Increase the value of count by 1 i.e. count++.
- Terminate the current line, i.e. System.out.println();
- Iterate for j=0 to j=rows and do the following :
Java Code for Floyd’s Triangle
import java.util.*; class PrepBuddy { public static void main(String[] args) { int n = 5; int i, j, k = 1; for (i = 1; i <= n; i++) { for (j = 1; j <= i; j++) { System.out.print(k + " "); k++; } System.out.println(); } } }
Output
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
Butterfly Pattern
To easily print the butterfly pattern in Java, we will split our code into two parts, i.e., print the upper part in the first go and then the second part.
The logic is as follows:
- Take input from the user on the number of rows in the Butterfly pattern. Let it be rows.
- Iterate from i=0 to i=rows-1 and do the following –
- Iterate from j=0 to j=i and print + Blank space i.e.i.e. System.out.print(" ");
- Define a variable to find the number of spaces needed to be printed; say it to be space.
- Initialize its value to be 2×(rows−i).
- Iterate from j=0 to j=spaces−1 and print blank spaces.
- Iterate from j=0 to j=i and print + Blank space i.e. System.out.print(" ");
- Terminate the line, i.e. System.out.println();
- Repeat the above-given steps, but this time Iterate from i=rows−1 to i=0. This will print the lower part, as we have already printed the upper part.
Java Code for Butterfly Pattern
import java.util.Scanner; class ButterFlyPatternExample { public static void drawButterflyPattern(int N) { int space = 2*N-1; int star = 0; for(int j = 1; j <= 2*N-1; j++){ if(j <= N){ space = space - 2; star++; } else { space = space + 2; star--; } for(int m = 1; m <= star; m++){ System.out.print("*"); } for(int n = 1; n <= space; n++){ System.out.print(" "); } for(int p = 1; p <= star; p++){ if(p != N){ System.out.print("*"); } } System.out.println(); } } public static void main(String[] args) { int N; Scanner sc = new Scanner(System.in); System.out.println("Enter value of N"); N = sc.nextInt(); sc.close(); drawButterflyPattern(N); } }
Output
* *
** **
*** ***
**** ****
*********
**** ****
*** ***
** **
* *
Full Pyramid
Printing the full pyramid is almost the same as printing the half pyramid. We need to print space before printing the *s.
The logic is as follows :
- Take input from the user on the number of rows in the triangle. Let it be rows.
- Iterate from i=0 to i=rows – 1 and do the following :
- Define a variable to find the number of spaces needed to be printed. Say it to be space.
- Initialize its value to be rows – i.
- Iterate from j=0 to j=space – 1 and print double spaces.
- Iterate from j=0 to j=2×i−1 and print *+space.
- Terminate the line i.e. System.out.println();
Java Code for Full Pyramid Pattern
Output:
*
* * *
* * * * *
* * * * * * *
Conclusion
All kinds of patterns can be easily printed by correctly using nested for loops (or while/do-while loops). To make it easier, code can be split into two parts, just like in the Butterfly pattern. In some pattern programs in Java, we should take out most care of blank spaces to make the pattern.
Other Java Programs
Java Program to Add Two Numbers
Java Program to Check Prime Number
Java Program to Check Whether a Number is a Palindrome or Not
Java Program to Find the Factorial of a Number
Java Program to Reverse a Number
Java Program to search an element in a Linked List
Program to convert ArrayList to LinkedList in Java
Java Program to Reverse a linked list
Java Program to search an element in a Linked List
Anagram Program in Java
Inheritance Program in Java
Even Odd Program in Java
Hello World Program in Java
If else Program in Java
Binary Search Program in Java
Linear Search Program in Java
Menu Driven Program in Java
Package Program in Java
Leap Year Program in Java
Array Programs in Java
Linked List Program in Java
String Programs in Java
Star Program in Java
Number Pattern Program in Java
For Loop Program In Java