Java is a widely-used programming language with a rich set of libraries that make it easy to create complex programs. Printing patterns is a common exercise in Java that helps students develop their programming skills. In this blog post, we will explore different ways of printing patterns in Java.
1: Square
2: Right Triangle
3: Inverted Right Triangle
4: Diamond
5: Inverted Diamond
6: Hourglass
7: Pyramid
8: Inverted Pyramid
9: Arrow
10: Hollow Square
11: Hollow Right Triangle
12: Hollow Inverted Right Triangle
13: Hollow Diamond
14: Hollow Inverted Diamond
15: Hollow Hourglass
Here are the Java code and the output for the five patterns you mentioned:
Pattern 1: Square
public class Pattern { public static void main(String[] args) { int n = 5; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { System.out.print("* "); } System.out.println(); } } }
Output:
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
Pattern 2: Right Triangle
public class Pattern { public static void main(String[] args) { int n = 5; for (int i = 1; i <= n; i++) { for (int j = 1; j <= i; j++) { System.out.print("* "); } System.out.println(); } } }
Output:
*
* *
* * *
* * * *
* * * * *
Learn How to Generate Random Numbers in Java, Now!
Pattern 3: Inverted Right Triangle
public class Pattern { public static void main(String[] args) { int n = 5; for (int i = n; i >= 1; i--) { for (int j = 1; j <= i; j++) { System.out.print("* "); } System.out.println(); } } }
Output:
* * * * *
* * * *
* * *
* *
*
Pattern 4: Diamond
public class Main { public static void main(String[] args) { int n = 5; int k = 0; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n - i; j++) { System.out.print(" "); } while (k != 2 * i - 1) { System.out.print("* "); k++; } k = 0; System.out.println(); } for (int i = n; i >= 1; i--) { for (int j = 1; j <= n - i; j++) { System.out.print(" "); } k = 0; while (k != 2 * i - 1) { System.out.print("* "); k++; } System.out.println(); } } }
Output:
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
Pattern 5: Inverted Diamond
public class Main { public static void main(String[] args) { int n = 5; int k = 0; for (int i = n; i >= 1; i--) { for (int j = 1; j <= n - i; j++) { System.out.print(" "); } while (k != 2 * i - 1) { System.out.print("* "); k++; } k = 0; System.out.println(); } for (int i = 2; i <= n; i++) { for (int j = 1; j <= n - i; j++) { System.out.print(" "); } k = 0; while (k != 2 * i - 1) { System.out.print("* "); k++; } System.out.println(); } } }
Output:
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
Know How to Print ASCII Value in Java, Here!
Pattern 6: Hourglass
public class HourglassPattern { public static void main(String[] args) { int n = 5; for (int i = n; i >= 1; i--) { for (int j = i; j < n; j++) { System.out.print(" "); } for (int k = 1; k <= 2 * i - 1; k++) { System.out.print("*"); } System.out.println(); } for (int i = 2; i <= n; i++) { for (int j = i; j < n; j++) { System.out.print(" "); } for (int k = 1; k <= 2 * i - 1; k++) { System.out.print("*"); } System.out.println(); } } }
Output:
*********
*******
*****
***
*
***
*****
*******
*********
Pattern 7: Pyramid
public class PyramidPattern { public static void main(String[] args) { int n = 5; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n - i; j++) { System.out.print(" "); } for (int k = 1; k <= 2 * i - 1; k++) { System.out.print("*"); } System.out.println(); } } }
Output:
*
***
*****
*******
*********
Pattern 8: Inverted Pyramid
public class InvertedPyramidPattern { public static void main(String[] args) { int n = 5; for (int i = n; i >= 1; i--) { for (int j = 1; j <= n - i; j++) { System.out.print(" "); } for (int k = 1; k <= 2 * i - 1; k++) { System.out.print("*"); } System.out.println(); } } }
Output:
*********
*******
*****
***
*
Pattern 9: Arrow
public class ArrowPattern { public static void main(String[] args) { int n = 5; for (int i = 1; i <= n; i++) { for (int j = 1; j <= i; j++) { System.out.print("*"); } System.out.println(); } for (int i = n - 1; i >= 1; i--) { for (int j = 1; j <= i; j++) { System.out.print("*"); } System.out.println(); } } }
Output:
*
**
***
****
*****
****
***
**
*
Pattern 10: Hollow Square
public class HollowSquare { public static void main(String[] args) { int n = 5; // change this value to adjust the size of the square for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { if (i == 1 || i == n || j == 1 || j == n) { System.out.print("* "); } else { System.out.print(" "); } } System.out.println(); } } }
Output:
* * * * *
* *
* *
* *
* * * * *
Pattern 11: Hollow Right Triangle
public class Main { public static void main(String[] args) { int n = 5; // change this value to adjust the height of the triangle for (int i = 1; i <= n; i++) { for (int j = 1; j <= i; j++) { if (i == n || j == 1 || j == i) { System.out.print("* "); } else { System.out.print(" "); } } System.out.println(); } } }
Output:
*
* *
* *
* *
* * * * *
Pattern 12: Hollow Inverted Right Triangle
public class Main { public static void main(String[] args) { int n = 5; // change this value to adjust the height of the triangle for (int i = 1; i <= n; i++) { for (int j = 1; j <= n - i + 1; j++) { if (i == 1 || j == 1 || j == n - i + 1) { System.out.print("* "); } else { System.out.print(" "); } } System.out.println(); } } }
Output:
* * * * *
* *
* *
* *
* *
Pattern 13: Hollow Diamond
public class Main { public static void main(String[] args) { int n = 5; // change this value to adjust the height of the diamond for (int i = 1; i <= n; i++) { for (int j = 1; j <= n - i; j++) { System.out.print(" "); } for (int j = 1; j <= 2 * i - 1; j++) { if (i == 1 || i == n || j == 1 || j == 2 * i - 1) { System.out.print("* "); } else { System.out.print(" "); } } System.out.println(); } for (int i = n - 1; i >= 1; i--) { for (int j = 1; j <= n - i; j++) { System.out.print(" "); } for (int j = 1; j <= 2 * i - 1; j++) { if (i == 1 || i == n || j == 1 || j == 2 * i - 1) { System.out.print("* "); } else { System.out.print(" "); } } System.out.println(); } } }
Output:
*
* *
* *
* *
* * * * * * * * *
* *
* *
* *
*
Pattern 14: Hollow Hourglass
public class Main { public static void main(String[] args) { int rows = 6; for (int i = 1; i <= rows; i++) { for (int j = 1; j <= i; j++) { System.out.print("* "); } for (int j = 1; j <= 2 * (rows - i); j++) { System.out.print(" "); } for (int j = 1; j <= i; j++) { System.out.print("* "); } System.out.println(); } for (int i = 1; i < rows; i++) { for (int j = 1; j <= rows - i; j++) { System.out.print("* "); } for (int j = 1; j <= 2 * i; j++) { System.out.print(" "); } for (int j = 1; j <= rows - i; j++) { System.out.print("* "); } System.out.println(); } } }
Output:
* *
* * * *
* * * * * *
* * * * * * * *
* * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * *
* * * * * * * *
* * * * * *
* * * *
* *
Number Pattern
1. Simple number program
import java.util.Scanner; public class Newtum { public static void printNums(int n) { int i, j,num; for(i=0; i<n; i++) // outer loop for rows { num=1; for(j=0; j<=i; j++) // inner loop for rows { // printing num with a space System.out.print(num+ " "); //incrementing value of num num++; } // ending line after each row System.out.println(); } } public static void main(String args[]) { int n = 5; printNums(n); } }
Output:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
2. Number Pattern Program in java
public class Main { public static void main(String[] args) { int i, j, k = 1; for (i = 1; i <= 5; i++) { for (j = 1; j< i + 1; j++) { System.out.print(k++ + " "); } System.out.println(); } } }
Output:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
3. Diamond Pattern Program in Java
public class Main { public static void main(String[] args) { for (int i = 1; i <= 4; i++) { int n = 4; for (int j = 1; j<= n - i; j++) { System.out.print(" "); } for (int k = i; k >= 1; k--) { System.out.print(k); } for (int l = 2; l <= i; l++) { System.out.print(l); } System.out.println(); } for (int i = 3; i >= 1; i--){ int n = 3; for (int j = 0; j<= n - i; j++) { System.out.print(" "); } for (int k = i; k >= 1; k--) { System.out.print(k); } for (int l = 2; l <= i; l++) { System.out.print(l); } System.out.println(); } } }
Output:
1
212
32123
4321234
32123
212
1
4. Descending order Pattern
public class Main { public static void main(String[] args) { int rows = 6; for (int i = rows; i >= 1; i--) { for (int j = rows; j >= i; j--) { System.out.print(j+" "); } System.out.println(); } } }
Output:
6
6 5
6 5 4
6 5 4 3
6 5 4 3 2
6 5 4 3 2 1
5. Right Triangle Numeric Pattern
public class Main { public static void main(String[] args) { int rows = 7; for (int i = 1; i <= rows; i++) { for (int j = i; j >= 1; j--) { System.out.print(j+" "); } System.out.println(); } } }
Output:
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
6 5 4 3 2 1
7 6 5 4 3 2 1
6. Zeros/ ones Pattern Programs
public class Main { public static void main(String[] args) { int rows =4; for (int i = 1; i <= rows; i++) { for (int j = 1; j <= i; j++) { if(j%2 == 0) { System.out.print(0); } else { System.out.print(1); } } System.out.println(); } } }
Output:
1
10
101
1010
8. Reverse square pattern:
public class Main { public static void main(String[] args) { int n = 3; for(int i=n; i>=1; i--){ for(int j=n; j>=1; j--){ System.out.print(i*j + " "); } System.out.println(); } } }
Output:
9 6 3
6 4 2
3 2 1
9. Reverse right-angled triangle pattern:
public class Main { public static void main(String[] args) { int n = 4; for(int i=n; i>=1; i--){ for(int j=i; j>=1; j--){ System.out.print(j + " "); } System.out.println(); } } }
Output:
4 3 2 1
3 2 1
2 1
1
10. Inverted right-angled triangle pattern:
public class Main { public static void main(String[] args) { int n = 5; for(int i=1; i<=n; i++){ for(int k=1; k<i; k++){ System.out.print(" "); } for(int j=i; j<=n; j++){ System.out.print(j + " "); } System.out.println(); } } }
Output:
1 2 3 4 5
2 3 4 5
3 4 5
4 5
5
11. Inverted reverse right-angled triangle pattern:
public class Main { public static void main(String[] args) { int n = 4; for(int i=1; i<=n; i++){ for(int k=1; k<i; k++){ System.out.print(" "); } for(int j=n; j>=i; j--){ System.out.print(j + " "); } System.out.println(); } } }
Output:
4 3 2 1
4 3 2
4 3
4
Get a Complete list of Online Certification Courses in India here!
Alphabet Program:
1. Right Triangle Alphabetic Pattern
public class Main { 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
A B C D E F G
2. Repeating Alphabet Pattern
public class Main { public static void main(String[] args) { int letter = 65; //ASCII value of capital A is 65 //inner loop for rwos for (int i = 0; i <= 9; i++) { //outer loop for columns for (int j = 0; j <= i; j++) { //prints the character System.out.print((char) letter + " "); } letter++; System.out.println(); } } }
Output:
A
B B
C C C
D D D D
E E E E E
F F F F F F
G G G G G G G
H H H H H H H H
I I I I I I I I I
J J J J J J J J J J
3. K-shape Alphabet Pattern
public class Main { public static void main(String[] args) { for (int i = 8; i >= 0; i--) { int alphabet = 65; for (int j = 0; j <= i; j++) { System.out.print((char)(alphabet + j) + " "); } System.out.println(); } for (int i = 0; i <= 8; i++) { int alphabet = 65; for (int j = 0; j <= i; j++) { System.out.print((char)(alphabet + j) + " "); } System.out.println(); } } }
Output:
A B C D E F G H
A B C D E F G
A B C D E F
A B C D E
A B C D
A B C
A B
A
A
A B
A B C
A B C D
A B C D E
A B C D E F
A B C D E F G
A B C D E F G H
A B C D E F G H I
4. Triangle Character Pattern
public class Main { public static void main(String[] args) { for (int i = 0; i <= 8; i++) { int alphabet = 65; for (int j = 8; j > i; j--) { System.out.print(" "); } for (int k = 0; k <= i; k++) { System.out.print((char)(alphabet + k) + " "); } 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
A B C D E F G
A B C D E F G H
A B C D E F G H I
5. Diamond Character Pattern
import java.util.Scanner; public class Main { public static void main(String[] args) { int rows = 5; System.out.println("Printing Diamond Alphabets Pattern\n"); int i, j, k, alphabet = 64; for (i = 1; i <= rows; i++) { for (j = 1; j <= rows - i; j++) { System.out.print(" "); } for (k = 1; k <= i * 2 - 1; k++) { System.out.print((char)(alphabet + k)); } System.out.println(); } for (i = rows - 1; i > 0; i--) { for (j = 1; j <= rows - i; j++) { System.out.print(" "); } for (k = 1; k <= i * 2 - 1; k++) { System.out.print((char)(alphabet + k)); } System.out.println(); } } }
Output:
Printing Diamond Alphabets Pattern
A
ABC
ABCDE
ABCDEFG
ABCDEFGHI
ABCDEFG
ABCDE
ABC
A
Now that Java has programs for implementing numeric patterns. Let’s continue by putting some character/alphabet patterns into practice.
Importance of pattern printing in Java
Java programming is essential for creating and manipulating patterns, and pattern printing is essential for a variety of reasons as follows:
- Develops problem-solving abilities: In order to solve a problem, a pattern printer must first divide it into smaller, more manageable components. This procedure improves a programmer’s problem-solving abilities, which can be used to tackle additional programming difficulties.
- Enhances program effectiveness: Pattern printing is a tool that Java developers can use to streamline and optimize their code. Programming tasks take less time and effort to complete because developers can create complex patterns with little code when they use loops and conditional statements.
- Fosters innovation: Programmers can express their creativity through pattern printing to create one-of-a-kind, intricate designs that can be applied to a variety of applications. Web pages, mobile applications, and graphical user interfaces are all examples of this.
- Establishes the basis for more complex programming: Programming basics like pattern printing serve as a foundation for more complex programming strategies. This covers the creation of algorithms, data structures, and object-oriented programming.
In conclusion, printing patterns in Java is an important skill for programmers to have. It allows them to create visually appealing designs and solve complex problems through algorithmic thinking. By understanding the basics of loops and conditional statements, developers can create a wide range of patterns and designs. With practice and experimentation, they can even develop their own unique patterns that are both aesthetically pleasing and functional.
Our blog post on “How to print patterns in Java” is intended to help you with any Java-related questions you may have. Visit Newtum’s website to learn more about our online coding courses in Java, Python, PHP, and other topics as you continue to improve your coding abilities.