Java Pattern Programs – Learn How to Print Pattern in Java
In many Java interviews Star, number, and character patterns are the most asked Java Pattern Programs to check your logical and coding skills. Pattern programs in Java help you to sharpen your looping concepts(for loop). Now if you are looking a place to get all the Java pattern exercises with solutions, then stop your search here.
Here we have compiled a top pattern program on Java. Now, remember that to learn pattern programs, you must know Java Loops.
Java Pattern Programs
Here, you will find the top 25 Java pattern programs with their proper code and explanation.
Table of Content
- 1. Square Hollow Pattern
- 2. Number triangle Pattern
- 3. Number-increasing Pyramid Pattern
- 4. Number-increasing reverse Pyramid Pattern
- 5. Number-changing Pyramid Pattern
- 6. Zero-One Triangle Pattern
- 7. Palindrome Triangle Pattern
- 8. Rhombus Pattern
- 9. Diamond Star Pattern
- 10. Butterfly Star Pattern
- 11. Square Fill Pattern
- 12. Right Half Pyramid Pattern
- 13. Reverse Right Half Pyramid Pattern
- 14. Left Half Pyramid Pattern
- 15. Reverse Left Half Pyramid Pattern
- 16. Triangle Star Pattern
- 17. Reverse number Triangle Pattern
- 18. Mirror Image Triangle Pattern
- 19. Hollow Triangle Pattern
- 20. Hollow Reverse Triangle Pattern
- 21. Hollow Diamond Pyramid
- 22. Hollow Hourglass Pattern
- 23. Pascal’s Triangle
- 24. Right Pascal’s Triangle
- 25. K Pattern
All Pattern Programs in Java are mentioned below:
1. Square Hollow Pattern
// Java Program to print pattern
// Square hollow pattern
import java.util.*;
public class w3wiki {
// Function to demonstrate pattern
public static void printPattern(int n)
{
int i, j;
// outer loop to handle number of rows
for (i = 0; i < n; i++) {
// inner loop to handle number of columns
for (j = 0; j < n; j++) {
// star will print only when it is in first
// row or last row or first column or last
// column
if (i == 0 || j == 0 || i == n - 1
|| j == n - 1) {
System.out.print("*");
}
// otherwise print space only.
else {
System.out.print(" ");
}
}
System.out.println();
}
}
// Driver Function
public static void main(String args[])
{
int n = 6;
printPattern(n);
}
}
Output
****** * * * * * * * * ******
2. Number triangle Pattern
// Java Program to print pattern
// Number triangle pattern
import java.util.*;
public class w3wiki {
// Function to demonstrate pattern
public static void printPattern(int n)
{
int i, j;
// outer loop to handle number of rows
for (i = 1; i <= n; i++) {
// inner loop to print space
for (j = 1; j <= n - i; j++) {
System.out.print(" ");
}
// inner loop to print star
for (j = 1; j <= i; j++) {
System.out.print(i + " ");
}
// print new line for each row
System.out.println();
}
}
// Driver Function
public static void main(String args[])
{
int n = 6;
printPattern(n);
}
}
Output
1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 6 6 6 6 6 6
3. Number-increasing Pyramid Pattern
// Java Program to print pattern
// Number-increasing pyramid
import java.util.*;
public class w3wiki {
// Function to demonstrate pattern
public static void printPattern(int n)
{
int i, j;
// outer loop to handle number of rows
for (i = 1; i <= n; i++) {
// inner loop to handle number of columns
for (j = 1; j <= i; j++) {
// printing column values upto the row
// value.
System.out.print(j + " ");
}
// print new line for each row
System.out.println();
}
}
// Driver Function
public static void main(String args[])
{
int n = 6;
printPattern(n);
}
}
Output
1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 1 2 3 4 5 6
4. Number-increasing reverse Pyramid Pattern
// Java Program to print pattern
// Number-increasing reverse pyramid
import java.util.*;
public class w3wiki {
// Function to demonstrate pattern
public static void printPattern(int n)
{
int i, j;
// outer loop to handle number of rows
for (i = n; i >= 1; i--) {
// inner loop to handle number of columns
for (j = 1; j <= i; j++) {
// printing column values upto the row
// value.
System.out.print(j + " ");
}
// print new line for each row
System.out.println();
}
}
// Driver Function
public static void main(String args[])
{
int n = 6;
printPattern(n);
}
}
Output
1 2 3 4 5 6 1 2 3 4 5 1 2 3 4 1 2 3 1 2 1
5. Number-changing Pyramid Pattern
// Java Program to print pattern
// Number-changing pyramid
import java.util.*;
// Java code for printing pattern
public class w3wiki {
// Function to demonstrate pattern
public static void printPattern(int n)
{
int i, j;
int num = 1;
// outer loop to handle number of rows
for (i = 1; i <= n; i++) {
// inner loop to handle number of columns
for (j = 1; j <= i; j++) {
// printing value of num in each iteration.
System.out.print(num + " ");
// increasing the value of num.
num++;
}
// printing new line for each row
System.out.println();
}
}
// Driver Function
public static void main(String args[])
{
int n = 6;
printPattern(n);
}
}
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
6. Zero-One Triangle Pattern
// Java Program to print pattern
// Zero-One triangle
import java.util.*;
public class w3wiki {
// Function to demonstrate pattern
public static void printPattern(int n)
{
int i, j;
//outer loop to handle number of rows
for (i = 1; i <= n; i++) {
//inner loop to handle number of columns
for (j = 1; j <= i; j++) {
// if the sum of (i+j) is even then print 1
if ((i + j) % 2 == 0) {
System.out.print(1 + " ");
}
// otherwise print 0
else {
System.out.print(0 + " ");
}
}
//printing new line for each row
System.out.println();
}
}
// Driver Function
public static void main(String args[])
{
int n = 6;
printPattern(n);
}
}
Output
1 0 1 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0 1
7. Palindrome Triangle Pattern
// Java Program to print pattern
// Palindrome triangle
import java.util.*;
public class w3wiki {
// Function to demonstrate pattern
public static void printPattern(int n)
{
int i, j;
// outer loop to handle number of rows
for (i = 1; i <= n; i++) {
// inner loop to print the spaces
for (j = 1; j <= 2 * (n - i); j++) {
System.out.print(" ");
}
// inner loop to print the first part
for (j = i; j >= 1; j--) {
System.out.print(j + " ");
}
// inner loop to print the second part
for (j = 2; j <= i; j++) {
System.out.print(j + " ");
}
// printing new line for each row
System.out.println();
}
}
// Driver Function
public static void main(String args[])
{
int n = 6;
printPattern(n);
}
}
Output
1 2 1 2 3 2 1 2 3 4 3 2 1 2 3 4 5 4 3 2 1 2 3 4 5 6 5 4 3 2 1 2 3 4 5 6
8. Rhombus Pattern
// Java Program to print
// Rhombus pattern
import java.util.*;
public class w3wiki {
// Function to demonstrate pattern
public static void printPattern(int n)
{
int i, j;
int num = 1;
// outer loop to handle number of rows
for (i = 1; i <= n; i++) {
// inner loop to print spaces
for (j = 1; j <= n - i; j++) {
System.out.print(" ");
}
// inner loop to print stars
for (j = 1; j <= n; j++) {
System.out.print("*");
}
// printing new line for each row
System.out.println();
}
}
// Driver Function
public static void main(String args[])
{
int n = 6;
printPattern(n);
}
}
Output
****** ****** ****** ****** ****** ******
9. Diamond Star Pattern
// Java Program to print
// Diamond Star Pattern
import java.util.*;
public class w3wiki {
// Function to demonstrate pattern
public static void printPattern(int n)
{
int i, j;
int num = 1;
// outer loop to handle upper part
for (i = 1; i <= n; i++) {
// inner loop to print spaces
for (j = 1; j <= n - i; j++) {
System.out.print(" ");
}
// inner loop to print stars
for (j = 1; j <= 2 * i - 1; j++) {
System.out.print("*");
}
System.out.println();
}
// outer loop to handle lower part
for (i = n-1; i >= 1; i--) {
// inner loop to print spaces
for (j = 1; j <= n - i; j++) {
System.out.print(" ");
}
// inner loop to print stars
for (j = 1; j <= 2 * i - 1; j++) {
System.out.print("*");
}
System.out.println();
}
}
// Driver Function
public static void main(String args[])
{
int n = 6;
printPattern(n);
}
}
Output
* *** ***** ******* ********* *********** ********* ******* ***** *** *
10. Butterfly Star Pattern
// Java Program to print
// Butterfly Pattern
import java.util.*;
// Java code for printing pattern
public class w3wiki {
// Function to demonstrate pattern
public static void printPattern(int n)
{
int i, j;
int num = 1;
// outer loop to handle upper part
for (i = 1; i <= n; i++) {
// inner loop to print stars
for (j = 1; j <= i; j++) {
System.out.print("*");
}
// inner loop to print spaces
int spaces = 2 * (n - i);
for (j = 1; j <= spaces; j++) {
System.out.print(" ");
}
// inner loop to print stars
for (j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
// outer loop to handle lower part
for (i = n; i >= 1; i--) {
// inner loop to print stars
for (j = 1; j <= i; j++) {
System.out.print("*");
}
// inner loop to print spaces
int spaces = 2 * (n - i);
for (j = 1; j <= spaces; j++) {
System.out.print(" ");
}
// inner loop to print stars
for (j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
}
// Driver Function
public static void main(String args[])
{
int n = 6;
printPattern(n);
}
}
Output
* * ** ** *** *** **** **** ***** ***** ************ ************ ***** ***** **** **** *** *** ** ** * *
11. Square Fill Pattern
// Java Program to print
// Square fill pattern
import java.util.*;
public class w3wiki {
// Function to demonstrate pattern
public static void printPattern(int n)
{
int i, j;
// outer loop to handle rows
for (i = 0; i <= n; i++) {
// inner loop to handle columns
for (j = 0; j <= n; j++) {
System.out.print("*");
}
// printing new line for each row
System.out.println();
}
}
// Driver Function
public static void main(String args[])
{
int n = 6;
printPattern(n);
}
}
Output
******* ******* ******* ******* ******* ******* *******
12. Right Half Pyramid Pattern
// Java Program to print
// Pyramid pattern
import java.util.*;
public class w3wiki {
// Function to demonstrate pattern
public static void printPattern(int n)
{
int i, j;
// outer loop to handle rows
for (i = 1; i <= n; i++) {
// inner loop to handle columns
for (j = 1; j <= i; j++) {
System.out.print("*");
}
// printing new line for each row
System.out.println();
}
}
// Driver Function
public static void main(String args[])
{
int n = 6;
printPattern(n);
}
}
Output
* ** *** **** ***** ******
13. Reverse Right Half Pyramid Pattern
// Java Program to print pattern
// Reverse Right Half Pyramid
import java.util.*;
public class w3wiki {
// Function to demonstrate pattern
public static void printPattern(int n)
{
int i, j;
// outer loop to handle rows
for (i = n; i >= 1; i--) {
// inner loop to handle columns
for (j = 1; j <= i; j++) {
System.out.print("*");
}
// printing new line for each row
System.out.println();
}
}
// Driver Function
public static void main(String args[])
{
int n = 6;
printPattern(n);
}
}
Output
****** ***** **** *** ** *
14. Left Half Pyramid Pattern
// Java Program to print pattern
// Left Half Pyramid pattern
import java.util.*;
public class w3wiki {
// Function to demonstrate pattern
public static void printPattern(int n)
{
int i, j;
// outer loop to handle rows
for (i = n; i >= 1; i--) {
// inner loop to print spaces.
for (j = 1; j < i; j++) {
System.out.print(" ");
}
// inner loop to print stars.
for (j = 0; j <= n - i; j++) {
System.out.print("*");
}
// printing new line for each row
System.out.println();
}
}
// Driver Function
public static void main(String args[])
{
int n = 6;
printPattern(n);
}
}
Output
* ** *** **** ***** ******
15. Reverse Left Half Pyramid Pattern
// Java Program to print pattern
// Reverse Left Half Pyramid
import java.util.*;
public class w3wiki {
// Function to demonstrate pattern
public static void printPattern(int n)
{
int i, j;
// calculating number of spaces
int num = 2 * n - 2;
// outer loop to handle rows
for (i = n; i > 0; i--) {
// inner loop to print spaces.
for (j = 0; j < n - i; j++) {
System.out.print(" ");
}
// Decrementing value of num after each loop
num = num - 2;
// inner loop to print stars.
for (j = 0; j < i; j++) {
System.out.print("*");
}
// printing new line for each row
System.out.println();
}
}
// Driver Function
public static void main(String args[])
{
int n = 6;
printPattern(n);
}
}
Output
****** ***** **** *** ** *
16. Triangle Star Pattern
// Java Program to print
// Triangular Pattern
import java.util.*;
public class w3wiki {
// Function to demonstrate pattern
public static void printPattern(int n)
{
int i, j;
// outer loop to handle rows
for (i = 0; i < n; i++) {
// inner loop to print spaces.
for (j = n - i; j > 1; j--) {
System.out.print(" ");
}
// inner loop to print stars.
for (j = 0; j <= i; j++) {
System.out.print("* ");
}
// printing new line for each row
System.out.println();
}
}
// Driver Function
public static void main(String args[])
{
int n = 6;
printPattern(n);
}
}
Output
* * * * * * * * * * * * * * * * * * * * *
17. Reverse number Triangle Pattern
// Java Program to print pattern
// Reverse number triangle
import java.util.*;
public class w3wiki {
// Function to demonstrate pattern
public static void printPattern(int n)
{
int i, j;
// outer loop to handle rows
for (i = 1; i <= n; i++) {
// inner loop to print spaces.
for (j = 1; j < i; j++) {
System.out.print(" ");
}
// inner loop to print value of j.
for (j = i; j <= n; j++) {
System.out.print(j + " ");
}
// printing new line for each row
System.out.println();
}
}
// Driver Function
public static void main(String args[])
{
int n = 6;
printPattern(n);
}
}
Output
1 2 3 4 5 6 2 3 4 5 6 3 4 5 6 4 5 6 5 6 6
18. Mirror Image Triangle Pattern
// Java Program to print pattern
// Mirror Image of a triangle
import java.util.*;
public class w3wiki {
// Function to demonstrate pattern
public static void printPattern(int n)
{
int i, j;
// Printing the upper part
for (i = 1; i <= n; i++) {
// inner loop to print spaces.
for (j = 1; j < i; j++) {
System.out.print(" ");
}
// inner loop to print value of j.
for (j = i; j <= n; j++) {
System.out.print(j + " ");
}
// printing new line for each row
System.out.println();
}
// Printing the lower part
for (i = n - 1; i >= 1; i--) {
// inner loop to print spaces.
for (j = 1; j < i; j++) {
System.out.print(" ");
}
// inner loop to print value of j.
for (j = i; j <= n; j++) {
System.out.print(j + " ");
}
// printing new line for each row
System.out.println();
}
}
// Driver Function
public static void main(String args[])
{
int n = 6;
printPattern(n);
}
}
Output
1 2 3 4 5 6 2 3 4 5 6 3 4 5 6 4 5 6 5 6 6 5 6 4 5 6 3 4 5 6 2 3 4 5 6 1 2 3 4 5 6
19. Hollow Triangle Pattern
// Java Program to print
// Hollow triangle pattern
import java.util.*;
public class w3wiki {
// Function to demonstrate pattern
public static void printPattern(int n)
{
int i, j, k;
// outer loop to handle rows
for (i = 1; i <= n; i++) {
// inner loop to print spaces.
for (j = i; j < n; j++) {
System.out.print(" ");
}
for (k = 1; k <= (2 * i - 1); k++) {
// printing stars.
if (k == 1 || i == n || k == (2 * i - 1)) {
System.out.print("*");
}
// printing spaces.
else {
System.out.print(" ");
}
}
System.out.println("");
}
}
// Driver Function
public static void main(String args[])
{
int n = 6;
printPattern(n);
}
}
Output
* * * * * * * * * ***********
20. Hollow Reverse Triangle Pattern
// Java Program to print pattern
// Reverse Hollow triangle
import java.util.*;
public class w3wiki {
// Function to demonstrate pattern
public static void printPattern(int n)
{
int i, j, k;
// outer loop to handle rows
for (i = n; i >= 1; i--) {
// inner loop to print spaces.
for (j = i; j < n; j++) {
System.out.print(" ");
}
for (k = 1; k <= (2 * i - 1); k++) {
// printing stars.
if (k == 1 || i == n || k == (2 * i - 1)) {
System.out.print("*");
}
// printing spaces.
else {
System.out.print(" ");
}
}
System.out.println("");
}
}
// Driver Function
public static void main(String args[])
{
int n = 6;
printPattern(n);
}
}
Output
*********** * * * * * * * * *
21. Hollow Diamond Pyramid
// Java Program to print Pattern
// Hollow Diamond Star
import java.util.*;
public class w3wiki {
// Function to demonstrate pattern
public static void printPattern(int n)
{
int i, j;
int num = 1;
// outer loop to handle upper part
for (i = 1; i <= n; i++) {
// inner loop to print spaces
for (j = 1; j <= n - i; j++) {
System.out.print(" ");
}
// inner loop to print stars
for (j = 1; j <= 2 * i - 1; j++) {
if (j == 1 || j == 2*i-1)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
// outer loop to handle lower part
for (i = n-1; i >= 1; i--) {
// inner loop to print spaces
for (j = 1; j <= n - i; j++) {
System.out.print(" ");
}
// inner loop to print stars
for (j = 1; j <= 2 * i - 1; j++) {
if (j == 1 || j == 2*i-1)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
// Driver Function
public static void main(String args[])
{
int n = 6;
printPattern(n);
}
}
Output
* * * * * * * * * * * * * * * * * * * *
22. Hollow Hourglass Pattern
// Java Program to print pattern
// Hollow Hourglass Pattern
import java.util.*;
public class w3wiki {
// Function to demonstrate pattern
public static void printPattern(int n)
{
int i, j;
// Printing the upper part
for (i = 1; i <= n; i++) {
// inner loop to print spaces.
for (j = 1; j < i; j++) {
System.out.print(" ");
}
// inner loop to print value of j.
for (j = i; j <= n; j++) {
if(j==i||j==n||i==1)
System.out.print("* ");
else
System.out.print(" ");
}
// printing new line for each row
System.out.println();
}
// Printing the lower part
for (i = n - 1; i >= 1; i--) {
// inner loop to print spaces.
for (j = 1; j < i; j++) {
System.out.print(" ");
}
// inner loop to print value of j.
for (j = i; j <= n; j++) {
if(j==i||j==n||i==1)
System.out.print("* ");
else
System.out.print(" ");
}
// printing new line for each row
System.out.println();
}
}
// Driver Function
public static void main(String args[])
{
int n = 6;
printPattern(n);
}
}
Output
* * * * * * * * * * * * * * * * * * * * * * * * * * * * *
23. Pascal’s Triangle
// Java Program to implement
// Pascal's Triangle
import java.util.*;
class GFG {
// Pascal function
public static void printPascal(int n)
{
for (int i = 1; i <= n; i++) {
for (int j = 0; j <= n - i; j++) {
// for left spacing
System.out.print(" ");
}
// used to represent x(i, k)
int x = 1;
for (int k = 1; k <= i; k++) {
// The first value in a line is always 1
System.out.print(x + " ");
x = x * (i - k) / k;
}
System.out.println();
}
}
// Driver code
public static void main(String[] args)
{
int n = 4;
printPascal(n);
}
}
Output
1 1 1 1 2 1 1 3 3 1
24. Right Pascal’s Triangle
// Java Program to print
// Right Pascal’s Triangle
import java.util.*;
// Java code for printing pattern
public class Main {
// Function to demonstrate pattern
public static void printPattern(int n)
{
int i, j;
int num = 1;
// outer loop to handle upper part
for (i = 1; i <= n; i++) {
// inner loop to print stars
for (j = 1; j <= i; j++) {
System.out.print("* ");
}
System.out.println();
}
// outer loop to handle lower part
for (i = n-1; i >= 1; i--) {
// inner loop to print stars
for (j = 1; j <= i; j++) {
System.out.print("* ");
}
System.out.println();
}
}
// Driver Function
public static void main(String args[])
{
int n = 4;
printPattern(n);
}
}
Output
* * * * * * * * * * * * * * * *
25. K Pattern
// Java Program to print pattern
// Reverse Right Half Pyramid
import java.util.*;
public class w3wiki {
// Function to demonstrate pattern
public static void printPattern(int n)
{
int i, j;
// outer loop to handle rows
for (i = n; i >= 1; i--) {
// inner loop to handle columns
for (j = 1; j <= i; j++) {
System.out.print("*");
}
// printing new line for each row
System.out.println();
}
// outer loop to handle rows
for (i = 2; i <= n; i++) {
// inner loop to handle columns
for (j = 1; j <= i; j++) {
System.out.print("*");
}
// printing new line for each row
System.out.println();
}
}
// Driver Function
public static void main(String args[])
{
int n = 6;
printPattern(n);
}
}
Output
****** ***** **** *** ** * ** *** **** ***** ******
Conclusion
Java pattern programs are a great way to learn and practice coding skills. They help you understand loops, nested loops, and how to think logically to solve problems. Whether you are a beginner or an experienced programmer, practicing pattern programs can improve your Java skills. So, keep coding, experimenting with different patterns, and enjoy the learning process
Java Pattern Programs – FAQ
What are Java Pattern Programs?
Java Pattern Programs are a set of programming exercises that involve creating various patterns using nested loops and print statements in the Java programming language. These programs help developers improve their understanding of control flow, loops, and logical thinking, which are essential skills in Java programming.
Why are Java Pattern Programs important?
Java Pattern Programs are important because they help developers develop problem-solving skills, logical thinking, and programming fundamentals. By creating different patterns, developers practice breaking down problems, identifying patterns, and implementing solutions using the core concepts of Java, such as loops, conditional statements, and print statements.
What are some common Java Pattern Programs?
Some common Java Pattern Programs include printing a half-pyramid, full pyramid, inverted pyramid, diamond pattern, and various alphabetic patterns. These patterns can be created using nested loops and print statements to display the desired shapes and characters.
How do you approach solving Java Pattern Programs?
To solve Java Pattern Programs, it’s important to understand the problem statement, identify the pattern, and then break down the problem into smaller, manageable steps. This involves determining the logic for the outer and inner loops, the conditions for printing the desired characters, and the overall structure of the pattern.
What are the benefits of practicing Java Pattern Programs?
Practicing Java Pattern Programs offers several benefits, such as improving problem-solving skills, enhancing logical thinking, reinforcing control flow concepts, and developing a better understanding of programming fundamentals. These programs also help build confidence in writing clean, efficient, and readable code.
How can I get started with Java Pattern Programs?
To get started with Java Pattern Programs, you can start with simple patterns and gradually increase the complexity. You can find numerous online resources, such as w3wiki exercises and coding challenges, that provide a wide range of Java Pattern Programs for you to practice and improve your skills.
Contact Us