Page Contents
Java pattern programs enhance the concepts of coding, logic, and loops. The main question in the Java interview is to test the logic and thinking of the programmer. We can print a Java pattern program in different designs but we must have a deep knowledge of the Java loop, such as for loop do-while loop.
We have grouped the Java pattern programs into three categories.
- Start Pattern
- Number Pattern
- Character Pattern
Example – Star Pattern
Program to print Right Triangle Star Pattern
public class RightTrianglePattern {
public static void main(String args[]) {
int i,j; // for columns
int row = 8; // row denotes the number of rows you want to print
for (i = 0; i < row; i++) {
for (j = 0; j <= i; j++) {
// prints stars
System.out.print("* ");
}
System.out.println(); // for new line after printing each line
}
}
}
Output

Print Left Triangle Star Pattern
public class LeftTriangle {
public static void main(String args[]) {
int i, j; // for columns
int row = 8; // the number of rows you want to print
for (i = 0; i < row; i++) {
for (j = 2 * (row - i); j >= 0; j--) {
// prints space between two stars
System.out.print(" ");
}
for (j = 0; j <= i; j++) {
// prints star
System.out.print("* ");
}
System.out.println();
}
}
}
Output

Print Pyramid Star Pattern
public class PyramidPattern {
public static void main(String args[]) {
int i, j; // for columns
int row = 8; // number of rows you want to print
for (i = 0; i < row; i++) {
for (j = row - i; j > 1; j--) {
// prints space between two stars
System.out.print(" ");
}
for (j = 0; j <= i; j++) {
// prints star
System.out.print("* ");
}
System.out.println();
}
}
}
Output

Example – Number Pattern
Program to print Number pattern – 1
public class NumberPattern1 {
public static void main(String args[]) {
int i, j, num;
int max = 9;
// loop for rows
for (i = 0; i < max; i++) {
num = 1;
// loop for columns
for (j = 0; j <= i; j++) {
// prints num
System.out.print(num + " ");
num=num+1;
}
System.out.println();
}
}
}
Output

Print Number pattern – 2
public class NumberPattern2 {
public static void main(String[] args) {
int i, j, k = 1;
for (i = 1; i <= 9; i++) {
for (j = 1; j < i + 1; j++) {
// prints value
System.out.print(k++ + " ");
}
System.out.println();
}
}
}
Output

Print Number Pattern – 3
public class NumberPattern3 {
public static void main(String args[]) {
int rows = 8;
for (int i = 1; i <= rows; i++) {
int num = i;
for (int j = 1; j <= i; j++) {
System.out.print(num + " ");
num = num + rows - j;
}
System.out.println();
}
}
}
Output

Example – Character Pattern
Program to print Right Triangle Alphabetic Pattern
public class RightAlphabaticPattern {
public static void main(String[] args) {
int alphabet = 65; // ASCII value of capital A
for (int i = 0; i <= 7; i++) {
for (int j = 0; j <= i; j++) {
System.out.print((char) (alphabet + j) + " ");
}
System.out.println();
}
}
}
Output

Print Repeating Alphabet Pattern
public class RepeatingPattern {
public static void main(String[] args) {
int letter = 65;
for (int i = 0; i <= 8; i++) {
for (int j = 0; j <= i; j++) {
System.out.print((char) letter + " ");
}
letter=letter+1;
System.out.println();
}
}
}
Output
