top

Search

Java Tutorial

.

UpGrad

Java Tutorial

Pattern Programs in Java

Introduction

“Pattern” is a relatively newer addition to Java. If you want to be a professional computer science expert or programmer, you must clearly understand pattern programs in Java. This article provides a holistic insight into what Pattern programs in Java mean.

Overview

This tutorial mainly deals with printing patterns in Java, the different patterns in Java, and pattern programs in Java. Read on to learn more!

How to Print Pattern in Java?

You can print different patterns using loops, such as for loops, while loops, and do-while loops in Java. Here are some examples of common patterns:

1. Print a right triangle of stars

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

2. Print a square of stars

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

3. Print a diamond of stars

public class Main {
    public static void main(String[] args) {
        int n = 5;
        // Print upper half of the diamond
        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();
        }
        // Print lower half of the diamond
        for (int i = n - 1; 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();
        }
    }
}

These are just a few examples, but you can print many other patterns in Java depending on your needs. The key is to use nested loops to iterate over the rows and columns of the pattern.

Star Patterns

Let us learn some more star pattern programs in Java:

Left Triangle Star Pattern

We have already covered the right triangle star pattern earlier in the article. Let us now check out the left triangle star pattern.

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

Pyramid Star Pattern

public class Main {
    public static void main(String[] args) {
        int rows = 5;
        for (int i = 1; i <= rows; i++) {
            // Print spaces
            for (int j = 1; j <= rows - i; j++) {
                System.out.print("  ");
            }
            // Print stars
            for (int k = 1; k <= 2 * i - 1; k++) {
                System.out.print("* ");
            }
            System.out.println();
        }
    }
}

Downward Triangle Star Pattern

public class Main {
    public static void main(String[] args) {
        int rows = 5;
        for (int i = rows; 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("* ");
            }
            System.out.println();
        }
    }
}

Mirrored Right Triangle Star Pattern

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

Right Down Mirror Star Pattern

public class Main {
    public static void main(String[] args) {
        int rows = 5;
        for (int i = rows; 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("* ");
            }
            System.out.println();
        }
    }
}

Sandglass Star Pattern

public class Main {
    public static void main(String[] args) {
        int rows = 5;
        // Upper Half
        for (int i = 1; i <= rows; i++) {
            for (int j = 1; j < i; j++) {
                System.out.print(" ");
            }
            for (int k = i; k <= rows; k++) {
                System.out.print("* ");
            }
            System.out.println();
        }
        // Lower Half
        for (int i = rows - 1; i >= 1; i--) {
            for (int j = 1; j < i; j++) {
                System.out.print(" ");
            }
            for (int k = i; k <= rows; k++) {
                System.out.print("* ");
            }
            System.out.println();
        }
    }
}

Number Patterns

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();
        }
    }
}

In this code, the outer for loop controls the number of rows in the pattern. It starts from i = 1 and goes up to rows, representing the total number of rows in the pattern.

Within each row, the inner for loop is responsible for printing the numbers. It starts from j = 1 and goes up to i, ensuring that the number of numbers printed in each row matches the current row number.

The number variable keeps track of the current number to be printed. It starts with 1 and increments after printing each number.

The System.out.println() statement is placed outside the inner loop to move to the next line after printing the numbers for each row.

Right Pascal's Triangle

public class Main {
    public static void main(String[] args) {
        int rows = 5;
        for (int i = 0; i < rows; i++) {
            int number = 1;
            // Print spaces
            for (int j = 0; j < rows - i - 1; j++) {
                System.out.print("  ");
            }
            // Print numbers
            for (int j = 0; j <= i; j++) {
                System.out.printf("%-4d", number);
                number = number * (i - j) / (j + 1);
            }
            System.out.println();
        }
    }
}

Left Pascal's Triangle

public class Main {
    public static void main(String[] args) {
        int rows = 5;
        for (int i = 0; i < rows; i++) {
            int number = 1;
            // Print numbers
            for (int j = 0; j <= i; j++) {
                System.out.printf("%-4d", number);
                number = number * (i - j) / (j + 1);
            }
            System.out.println();
        }
    }
}

Character Patterns

Let us learn some character pattern programs in Java:

Right Triangle Alphabetic Pattern

public class Main {
    public static void main(String[] args) {
        int rows = 5;
        char currentChar = 'A';
        for (int i = 1; i <= rows; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(currentChar + " ");
                currentChar++;
            }
            System.out.println();
        }
    }
}

Repeating Alphabet Pattern

public class Main {
    public static void main(String[] args) {
        int rows = 5;
        char currentChar = 'A';
        for (int i = 1; i <= rows; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(currentChar + " ");
                currentChar++;
                if (currentChar > 'Z') {
                    currentChar = 'A';
                }
            }
            System.out.println();
        }
    }
}

Java Pattern Programs

Here are some more Java pattern programs:

Square Hollow Pattern

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

Number-increasing Pyramid Pattern

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 <= rows - i; j++) {
                System.out.print("  ");
            }
            for (int k = 1; k <= i; k++) {
                System.out.print(number + "   ");
                number++;
            }
            System.out.println();
        }
    }
}

Number-increasing reverse Pyramid Pattern

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

K-shape Character Pattern

public class Main {
    public static void main(String[] args) {
        int rows = 5;
        int spaces = rows - 1;
        int midRow = rows / 2;
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < spaces; j++) {
                System.out.print("  ");
            }
            System.out.print("* ");
            if (i >= midRow) {
                int innerSpaces = 2 * (i - midRow) + 1;
                for (int j = 0; j < innerSpaces; j++) {
                    System.out.print("  ");
                }
                System.out.print("*");
            }
            System.out.println();
            spaces--;
        }
    }
}

Zero-One triangle Pattern

public class Main {
    public static void main(String[] args) {
        int rows = 5;
        for (int i = 1; i <= rows; i++) {
            for (int j = 1; j <= i; j++) {
                int value = (i + j) % 2;
                System.out.print(value + " ");
            }
            System.out.println();
        }
    }
}

Palindrome triangle Pattern

public class Main {
    public static void main(String[] args) {
        int rows = 5;
        for (int i = 1; i <= rows; i++) {
            int num = 1;
            for (int j = 1; j <= rows - i; j++) {
                System.out.print("  ");
            }
            for (int k = 1; k <= i; k++) {
                System.out.print(num + " ");
                num++;
            }
            num -= 2;
            for (int k = 1; k <= i - 1; k++) {
                System.out.print(num + " ");
                num--;
            }
            System.out.println();
        }
    }
}

Rhombus Pattern

public class Main {
    public static void main(String[] args) {
        int rows = 5;
        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("* ");
            }
            System.out.println();
        }
        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("* ");
            }
            System.out.println();
        }
    }
}

Reverse Right Half Pyramid Pattern

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

Hollow Diamond Pyramid

public class Main {
    public static void main(String[] args) {
        int rows = 5;
        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++) {
                if (k == 1 || k == 2 * i - 1) {
                    System.out.print("*");
                } else {
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
        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++) {
                if (k == 1 || k == 2 * i - 1) {
                    System.out.print("*");
                } else {
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
    }
}

Hollow Hourglass Pattern

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

Conclusion

This tutorial provided a glimpse of what pattern programs in Java are with several examples. It is an important part of Java, and you can master it with the help of materials available on the internet. If you need further help and want to learn to program professionally, enroll in one of the many computer science courses offered by learning platforms like upGrad.

FAQs 

1. What are some common patterns that can be printed in Java?

Some common patterns include triangles (right, isosceles, equilateral), squares (filled, hollow), rectangles, diamonds, pyramids, and zigzag lines. A number triangle pattern in Java is also common.

2. How do I print a pattern in reverse order?

To print a pattern in reverse order, you can use nested loops and adjust the loop conditions and the order of printing the characters or strings to reverse the pattern.

3. How do I print a pattern of numbers instead of characters or strings?

You can use nested loops to replace the character or printed string with the desired number. You can also use arithmetic operations or conditional statements to print a specific pattern of numbers.

Leave a Reply

Your email address will not be published. Required fields are marked *