• Articles
  • Tutorials
  • Interview Questions

25 Java Pattern Programs with Source Code

25 Java Pattern Programs with Source Code

Are you prepared to advance your knowledge of Java programming? Then, learn how powerful Java pattern can be. Enter the world where functionality and elegant coding coexist. You can practice your coding skills by answering interview questions based on these pattern, which are frequently asked. Numerous pattern, such as stars, numbers, and characters, will be discussed. With the help of this blog, you will be able to develop your own Java pattern programs.

Table of Contents:

Want to learn Java in detail? Here’s a video for you by Intellipaat!

Video Thumbnail

What are Java patterns?

In the context of Java programming, the term “pattern” typically refers to the visual arrangements of characters or symbols printed on the console through a combination of loops and conditional statements. These pattern serve as a creative and educational way to understand and practice programming logic, especially involving nested loops. pattern can range from basic geometric shapes, such as squares and triangles, to more complex designs, including numerical pattern like Pascal’s triangle, or visually appealing pattern like spirals and diamonds.

Creating Java pattern involves using loops to control the repetition of characters or symbols and determining their positions and relationships. These exercises help programmers develop a solid understanding of nested loops, conditional statements, and control flow in Java. 

pattern are often utilized in educational settings to reinforce programming concepts and encourage problem-solving skills. Additionally, pattern programming can be applied in real-world scenarios where understanding and manipulating structured data are essential skills. Overall, Java pattern serve as a practical and engaging tool for honing programming skills and enhancing logical thinking.

Java patterns

Basic Java pattern

Basic Java pattern typically involve using nested loops to create simple geometric shapes like squares, triangles, or pyramids. These pattern serve as fundamental exercises for beginners, helping them grasp concepts such as loop structures and conditional statements while also building a foundation for more complex pattern programming in Java.

To run the provided codes in Visual Studio Code, you have two options. First, create a single file named “Main.java” within the Java folder and execute the code freely. Alternatively, customize the public class name to your preference and create a separate folder with the same name. In Java programming, it’s important to note that the main class name and the file name should match.

Square Pattern

Here’s a code for printing a square pattern in Java:

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter the size of the square: ");
        int size = scanner.nextInt();
        // Loop to iterate through rows
        for (int i = 1; i <= size; i++) {
            // Loop to iterate through columns
            for (int j = 1; j <= size; j++) {
                System.out.print("* ");
            }
            // Move to the next line after each row
            System.out.println();
        }
        scanner.close();
    }
}

Output: 

Right-Angled Triangle Pattern

Following is the code to create a right-angled triangle pattern:

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter the height of the right-angled triangle: ");
        int height = scanner.nextInt();
        // Loop to iterate through rows
        for (int i = 1; i <= height; i++) {
            // Loop to iterate through columns
            for (int j = 1; j <= i; j++) {
                System.out.print("* ");
            }
            // Move to the next line after each row
            System.out.println();
        }
        scanner.close();
    }
}

Output:

Inverted Right Angle Triangle Pattern

Given below is the code for the inverted right-angle triangle pattern:

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter the height of the inverted right-angled triangle: ");
        int height = scanner.nextInt();
        // Loop to iterate through rows in reverse order
        for (int i = height; i >= 1; i--) {
            // Loop to iterate through columns
            for (int j = 1; j <= i; j++) {
                System.out.print("* ");
            }
            // Move to the next line after each row
            System.out.println();
        }
        scanner.close();
    }
}

Output:

Pyramid Pattern

The code for printing a pyramid pattern is as follows:

import java.util.*; 
public class Main { 
    // 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:

Number Pyramid Pattern

Here’s a code for printing a number pyramid pattern: 

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter the height of the number pyramid: ");
        int height = scanner.nextInt();
        // Loop to iterate through rows
        for (int i = 1; i <= height; i++) {
            // Loop to print leading spaces
            for (int j = 1; j <= height - i; j++) {
                System.out.print("  ");
            }
            // Loop to print numbers
            for (int k = 1; k <= 2 * i - 1; k++) {
                System.out.print(k + " ");
            }
            // Move to the next line after each row
            System.out.println();
        }
        scanner.close();
    }
}

Output:

Alphabetic Pyramid Pattern

Here’s an example of a Java code that generates a simple alphabetic pattern: 

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        int rowCount = 5; // Number of rows in the pyramid
        for (int i = 0; i < rowCount; i++) {
            char ch = 'A'; // Starting character
            for (int j = 0; j <= i; j++) {
                System.out.print(ch + " "); // Print the characters
                ch++;
            }
            System.out.println(); // Move to the next line
        }
    }
}

Output:

Get 100% Hike!

Master Most in Demand Skills Now!

Intermediate Java pattern

These pattern often involve complex shapes and designs created using nested loops and conditional statements. These pattern move beyond basic shapes like triangles or squares, incorporating more challenging structures like diamonds, crosses, or pattern with diagonal stripes. They require a deeper understanding of loop structures, conditional logic, and the manipulation of iteration variables to create specific arrangements of characters or symbols.

Floyd’s Triangle Pattern

Here’s an example of Java code that generates a Floyd’s triangle pattern:

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        int rows = 5;
        int number = 1;
        for (int i = 1; i <= rows; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(number + " ");
                number++;
            }
            System.out.println();
        }
    }
}

Output:

Pascal’s Triangle Pattern

Below is the code for printing a Pascal’s triangle:

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        int rows = 5;
        for (int i = 0; i < rows; i++) {
            int number = 1;
            for (int j = 0; j <= i; j++) {
                System.out.print(number + " ");
                number = number * (i - j) / (j + 1);
            }
            System.out.println();
        }
    }
}

Output:

Hollow Diamond Pattern

Follow the code for printing a hollow diamond pattern:

Output:

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        int rows = 5;
        // Upper part of the diamond
        for (int i = 1; i <= rows; i++) {
            for (int j = i; j < rows; j++) {
                System.out.print(" ");
            }
            for (int j = 1; j <= (2 * i - 1); j++) {
                if (j == 1 || j == (2 * i - 1)) {
                    System.out.print("*");
                } else {
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
        // Lower part of the diamond
        for (int i = rows - 1; i >= 1; i--) {
            for (int j = rows; j > i; j--) {
                System.out.print(" ");
            }
            for (int j = 1; j <= (2 * i - 1); j++) {
                if (j == 1 || j == (2 * i - 1)) {
                    System.out.print("*");
                } else {
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
    }
}

Diamond Pattern

To print a diamond design, follow this code:

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        int rows = 5;
        int spaces = rows - 1;
        // Upper part of the diamond
        for (int i = 1; i <= rows; i++) {
            for (int j = 1; j <= spaces; j++) {
                System.out.print(" ");
            }
            spaces--;
            for (int j = 1; j <= 2 * i - 1; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
        spaces = 1;
        // Lower part of the diamond
        for (int i = 1; i <= rows - 1; i++) {
            for (int j = 1; j <= spaces; j++) {
                System.out.print(" ");
            }
            spaces++;
            for (int j = 1; j <= 2 * (rows - i) - 1; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

Output:

Hollow Pyramid Pattern

Use the given code to print a hollow pyramid pattern:

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        int rows = 5;
        for (int i = 1; i <= rows; i++) {
            for (int j = i; j < rows; j++) {
                System.out.print(" ");
            }
            for (int k = 1; k <= (2 * i - 1); k++) {
                if (k == 1 || k == 2 * i - 1 || i == rows) {
                    System.out.print("*");
                } else {
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
    }
}

Output:

Fibonacci Pattern 

For printing the Fibonacci pattern, use the provided code:

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        int n = 9; // Number of lines in the pattern
        int a = 0, b = 1;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j <= i; j++) {
                System.out.print(a + " ");
                int sum = a + b;
                a = b;
                b = sum;
            }
            System.out.println();
        }
    }
}

Output:

Butterfly Pattern

Print the hollow pyramid design using the provided code:

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        int n = 5; // Number of lines in the pattern
        for (int i = 0; i < n; i++) {
            for (int j = 0; j <= i; j++) {
                System.out.print("* ");
            }
            int spaces = 2 * (n - i - 1);
            for (int j = 0; j < spaces; j++) {
                System.out.print("  ");
            }
            for (int j = 0; j <= i; j++) {
                System.out.print("* ");
            }
            System.out.println();
        }
        for (int i = n - 1; i >= 0; i--) {
            for (int j = 0; j <= i; j++) {
                System.out.print("* ");
            }
            int spaces = 2 * (n - i - 1);
            for (int j = 0; j < spaces; j++) {
                System.out.print("  ");
            }
            for (int j = 0; j <= i; j++) {
                System.out.print("* ");
            }
            System.out.println();
        }
    }
}

Output:

Rhombus Pattern

Below is the code for printing the Rhombus Pattern:

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        int n = 5; // Number of lines in the pattern
        for (int i = 1; i <= n; i++) {
            // Print spaces
            for (int j = 1; j <= n - i; j++) {
                System.out.print(" ");
            }
            // Print stars
            for (int j = 1; j <= n; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

Output:

Hollow Square Pattern

Here’s the code for the hollow square pattern:

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        int rows = 5; // Number of rows in the pattern
        for (int i = 1; i <= rows; i++) {
            for (int j = 1; j <= rows; j++) {
                // Print '*' at boundaries or at the center
                if (i == 1 || i == rows || j == 1 || j == rows) {
                    System.out.print("*");
                } else {
                    System.out.print(" ");
                }
                System.out.print(" ");
            }
            System.out.println();
        }
    }
}

Output:

Advanced Java pattern

Advanced Java pattern deal with more sophisticated designs, often involving complex geometrical shapes, fractals, or pattern with more symmetries. These pattern require a deep understanding of algorithms, mathematical concepts, and advanced data structures. Advanced Java pattern challenge programmers to think critically, employing advanced programming techniques and mathematical principles to craft elaborate and visually stunning pattern. Mastery of these pattern not only showcases expertise in Java programming but also demonstrates a profound grasp of algorithmic thinking and problem-solving skills within the context of creative visual output.

Spiral Pattern

Here is the code to print a spiral design made up of numbers: 

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        int size = 5; // Define the size of the pattern
        int[][] spiral = new int[size][size];
        int value = 1;
        int minCol = 0;
        int maxCol = size - 1;
        int minRow = 0;
        int maxRow = size - 1;
        while (value <= size * size) {
            for (int i = minCol; i <= maxCol; i++) {
                spiral[minRow][i] = value;
                value++;
            }
            for (int i = minRow + 1; i <= maxRow; i++) {
                spiral[i][maxCol] = value;
                value++;
            }
            for (int i = maxCol - 1; i >= minCol; i--) {
                spiral[maxRow][i] = value;
                value++;
            }
            for (int i = maxRow - 1; i >= minRow + 1; i--) {
                spiral[i][minCol] = value;
                value++;
            }
            minCol++;
            minRow++;
            maxCol--;
            maxRow--;
        }
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                System.out.print(spiral[i][j] + "\t");
            }
            System.out.println();
        }
    }
}

Output:

Cross Pattern

Here is a code that prints a cross pattern of numbers: 

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        int size = 5; // Define the size of the pattern
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                if (i == size / 2 || j == size / 2) {
                    System.out.print("* ");
                } else {
                    System.out.print("  ");
                }
            }
            System.out.println();
        }
    }
}

Output:

Zig Zag Pattern

This Java code prints a zig-zag pattern:

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter the number of characters in a line: ");
        int charactersInLine = scanner.nextInt();
        System.out.print("Enter the number of zigzag lines: ");
        int zigzagLines = scanner.nextInt();
        for (int i = 1; i <= zigzagLines; i++) {
            for (int r = 1; r <= charactersInLine; r++) {
                for (int c = 1; c <= charactersInLine; c++) {
                    if (r == c) {
                        System.out.print(r + " ");
                    } else {
                        System.out.print("  ");
                    }
                }
                System.out.println();
            }
            for (int r = 1; r <= charactersInLine; r++) {
                for (int c = 1; c <= charactersInLine; c++) {
                    if (c <= (charactersInLine + 1 - r)) {
                        if (c == (charactersInLine + 1 - r)) {
                            System.out.print(r + " ");
                        } else {
                            System.out.print("  ");
                        }
                    }
                }
                System.out.println();
            }
        }
    }
}

Output:

Hourglass Pattern

To print an hourglass pattern in Java, use this code:

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter the number of rows in the hourglass pattern: ");
        int rows = scanner.nextInt();
        int spaces = 0;
        // Upper part of the hourglass
        for (int i = rows; i >= 1; i--) {
            for (int j = 0; j < spaces; j++) {
                System.out.print(" ");
            }
            for (int k = 1; k <= 2 * i - 1; k++) {
                System.out.print("*");
            }
            System.out.println();
            spaces++;
        }
        // Lower part of the hourglass
        spaces = rows - 1;
        for (int i = 1; i <= rows; i++) {
            for (int j = 0; j < spaces; j++) {
                System.out.print(" ");
            }
            for (int k = 1; k <= 2 * i - 1; k++) {
                System.out.print("*");
            }
            System.out.println();
            spaces--;
        }
    }
}

Output:

Puzzle Pattern

The following Java code prints a puzzle pattern:

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        // Get user input for the size of the pattern
        System.out.println("Enter the size of the pattern:");
        int size = scanner.nextInt();
        // Generate and display the puzzle pattern
        generatePuzzlePattern(size);
        scanner.close();
    }
    private static void generatePuzzlePattern(int size) {
        for (int i = 1; i <= size; i++) {
            for (int j = 1; j <= size; j++) {
                // Check if the current position is on the border or inside the pattern
                if (i == 1 || i == size || j == 1 || j == size || (i > size / 4 && i <= 3 * size / 4 && j > size / 4 && j <= 3 * size / 4)) {
                    System.out.print("* ");
                } else {
                    System.out.print("  ");
                }
            }
            System.out.println();
        }
    }
}

Output:

K Pattern

Here is some Java code to print a K pattern:

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter the Value for n: ");
        int n = scanner.nextInt();
        for (int i = -n; i <= n; i++) {
            int k = i < 0 ? -i : i;
            for (int j = 0; j <= n; j++) {
                if (k >= j) {
                    System.out.print("* ");
                } else {
                    System.out.print("  "); // Two spaces for Java's equivalent to one space in printf
                }
            }
            System.out.println();
        }
    }
}

Output:

Arrow Pattern

Print an arrow design by using the code provided below:

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter the size of the arrow pattern: ");
        int size = scanner.nextInt();
        // Upper part of the arrow pattern
        for (int i = 1; i <= size; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print("* ");
            }
            System.out.println();
        }
        // Lower part of the arrow pattern
        for (int i = size - 1; i >= 1; i--) {
            for (int j = 1; j <= i; j++) {
                System.out.print("* ");
            }
            System.out.println();
        }
    }
}

Output:

Diamond with Numbers Pattern

To print a diamond pattern with numbers, refer to the code below:

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter the number of rows for the diamond pattern: ");
        int rows = scanner.nextInt();
        // Upper part of the diamond pattern
        for (int i = 1; i <= rows; i++) {
            for (int j = 1; j <= rows - i; j++) {
                System.out.print("  ");
            }
            for (int k = 1; k <= 2 * i - 1; k++) {
                System.out.print(k + " ");
            }
            System.out.println();
        }
        // Lower part of the diamond pattern
        for (int i = rows - 1; i >= 1; i--) {
            for (int j = 1; j <= rows - i; j++) {
                System.out.print("  ");
            }
            for (int k = 1; k <= 2 * i - 1; k++) {
                System.out.print(k + " ");
            }
            System.out.println();
        }
    }
}

Output:

Circular Spiral Pattern

Refer to the code below for printing a circular spiral pattern:

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter the size of the circular spiral pattern: ");
        int size = scanner.nextInt();
        int[][] spiral = new int[size][size];
        int value = 1;
        int minRow = 0, maxRow = size - 1, minCol = 0, maxCol = size - 1;
        while (value <= size * size) {
            for (int i = minCol; i <= maxCol && value <= size * size; i++) {
                spiral[minRow][i] = value++;
            }
            minRow++;
            for (int i = minRow; i <= maxRow && value <= size * size; i++) {
                spiral[i][maxCol] = value++;
            }
            maxCol--;
            for (int i = maxCol; i >= minCol && value <= size * size; i--) {
                spiral[maxRow][i] = value++;
            }
            maxRow--;
            for (int i = maxRow; i >= minRow && value <= size * size; i--) {
                spiral[i][minCol] = value++;
            }
            minCol++;
        }
        // Printing the spiral pattern
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                System.out.printf("%-4d", spiral[i][j]);
            }
            System.out.println();
        }
    }
}

Output:

Diamond with Diagonal Stripes Pattern

Here’s an example of a Java code that generates a diamond with a diagonal stripes pattern:

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter the size of the diamond pattern: ");
        int size = scanner.nextInt();
        int totalColumns = (size * 2) - 1;
        // Upper part of the diamond pattern
        for (int i = 1; i <= size; i++) {
            int count = 1;
            for (int j = 1; j <= totalColumns; j++) {
                if (j >= size - (i - 1) && j <= size + (i - 1)) {
                    if (count % 2 == 0) {
                        System.out.print("-");
                    } else {
                        System.out.print("*");
                    }
                    count++;
                } else {
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
        // Lower part of the diamond pattern
        for (int i = size - 1; i >= 1; i--) {
            int count = 1;
            for (int j = 1; j <= totalColumns; j++) {
                if (j >= size - (i - 1) && j <= size + (i - 1)) {
                    if (count % 2 == 0) {
                        System.out.print("-");
                    } else {
                        System.out.print("*");
                    }
                    count++;
                } else {
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
    }
}

Output:

Wrap-up

Java pattern programs serve as an excellent way to refine programming skills, offering a practical stage for mastering loops, conditional statements, and problem-solving strategies. Proficiency in crafting these pattern not only showcases technical expertise but also highlights one’s ability to tackle complex challenges creatively. In a professional setting, this skill proves invaluable, helping in code optimization, algorithm development, and efficient problem-solving. Moreover, in the business field, these skills can be applied to data visualization, creating appealing user interfaces, and generating dynamic content for marketing or data analysis. Looking ahead, the future scope holds promise as these skills align with the increasing demand for data visualization, user experience design, and algorithmic thinking in various technological domains.

Course Schedule

Name Date Details
Python Course 14 Dec 2024(Sat-Sun) Weekend Batch View Details
21 Dec 2024(Sat-Sun) Weekend Batch
28 Dec 2024(Sat-Sun) Weekend Batch

About the Author

Senior Consultant Analytics & Data Science

Sahil Mattoo, a Senior Software Engineer at Eli Lilly and Company, is an accomplished professional with 14 years of experience in languages such as Java, Python, and JavaScript. Sahil has a strong foundation in system architecture, database management, and API integration.