The Ultimate Pattern Programs in Java

by  
Share:
The Ultimate Pattern Programs in Java

Students preparing to learn the Java programming language and want to clear their fundamentals concerning loop construct, decision-making statements & control statements through coding various number and star pattern programs in Java should read this book.

Java is not magical, it’s a tool for people to build stuff. It’s the hammer and nail. It’s turns out to be easy to do things like AI and other fairly intelligent systems. – James Gosling (Creator of Java)

Book Overview

I am pleased to inform you that this book contains 100 programs with source codes available in separate text files arranged as per their order.

The book is divided into five sections arranged in a very easy-to-learn manner. After assessing as well as practicing everything inside the book, I ensure you will certainly understand this topic.

  1. Warmup Exercise – This section has 5 programs to help you understand the working of for loops, grid systems, and star pattern programs in Java.
  2. Geometrical Set – This section has 30 programs. I have organized them in easy-to-learn order, i.e., the simple pattern first and the complex pattern last. You can print various geometrical patterns, like pyramid patterns, half pyramid patterns, diamond patterns, hollow diamond patterns, and so on.
  3. Numbers Set – This section consists of 10 programs. You will be printing all the numbers from 0 to 9 with the star pattern.
  4. Alphabet Set – There are 26 programs in this section. You will learn to print letters from A to Z with star patterns.
  5. Mixed Set – There are 29 programs in this section. It has various programs like pyramid pattern of numbers, reverse pyramid patterns, inverted pyramid patterns, hollow diamond pattern programs in Java.

If you want a book that covers every aspect of pattern programs in C, including loops, break, and continue statements.

Don’t waste your time looking for anything else. Purchase this book at the cost of less than a cup of coffee.

Buy now

Programs from the Book

I am listing down a few programs from the book, and if you like the content, you can purchase the book.

Java program to print table from 1 to 10

Java program to print table from 1 to 10

In this Java program, we need to print the table from 1 to 10. But before we get to code, let us first understand the program logic.

  • Take ten rows and ten columns.
  • Start an outer loop (row) that iterate ten times.
  • Under the outer loop start an inner loop (column), that iterates 10 times.
  • Multiply the current row value with the current column value inside the inner loop.
public class Pattern {
	public static void main(String[] args) {
		int row, column;
		for (row = 1; row <= 10; row++) {
			for (column = 1; column <= 10; column++) {
				System.out.printf("%3d ", (row * column));
			}
			System.out.println();
		}
	}

}

Program to print square with star pattern in Java

print square with star pattern

To print the square pattern, use the logic given below.

  • Take five rows and five columns.
  • Start an outer loop (row) that will iterate five times.
  • Under the outer loop start an inner loop (column) that iterate the same number of times
  • Inside the inner loop print star.
public class Pattern {
	public static void main(String[] args) {
		int row, column;
		int row_length = 5;
		int column_length = 5;

		for (row = 1; row <= row_length; row++) {
			for (column = 1; column <= column_length; column++) {
				System.out.print("* ");
			}
			System.out.println();
		}
	}
}

Print square with number pattern program in Java

Print square with number pattern

  • Take five rows and five columns.
  • Start an outer loop (row) that will iterate five times.
  • Under the outer loop start an inner loop (column) that iterate the same number of times
  • Inside the inner loop print current column value.
public class Pattern {
	public static void main(String[] args) {
		int row, column;
		int row_length = 5;
		int column_length = 5;

		for (row = 1; row <= row_length; row++) {
			for (column = 1; column <= column_length; column++) {
				System.out.printf("%d ", row);;
			}
			System.out.println();
		}
	}
}

Print square with alphabet pattern program in Java

Print square with alphabet pattern

  • Take five rows and five columns.
  • Start an outer loop (row) that will iterate five times.
  • Under the outer loop start an inner loop (column) that iterate the same number of times
  • The ASCII code for capital A is 65 and small a is 97. Use whichever you prefer.
  • Set a variable ascii_code_start to 65 and increment by 1 after each row iteration.
public class Pattern {
	public static void main(String[] args) {
		int row, column; 
	        int row_length = 5;
		int column_length = 5; 
		int ascii_code_start = 65;
		
		for (row = 1; row <= row_length; row++) {
			for (column = 1; column <= column_length; column++) {
				System.out.printf("%c ", ascii_code_start);
			}
			ascii_code_start++;
			System.out.println();
		}
	}
}

Program to print rhombus with star pattern in Java

print rhombus with star pattern

  • The blank spaces in the grid (marked in yellow) will come in play here.
  • Observe, as rows increase, space decreases. Row 1 has 5 spaces; Row 2 has 4 spaces.
  • Start an outer loop (row) that will iterate five times.
  • Under the outer loop, start an inner loop (for spaces) that iterate till total row length minus current row value.
  • After the inner loop for spaces, start loop again (for star) that iterate five times.
  • Print star inside this loop.
public class Pattern {
	public static void main(String[] args) {
	    int row, column, spaces; 
	    int row_length = 10;
	    
	    for (row = 0; row < row_length; row++) {
	    	for (spaces = 1; spaces <= row_length - row ; spaces++) {
	    		System.out.print(" ");
	        }
	        for (column = 1; column <= row_length ; column++) {
	        	System.out.print("*");
	        }
	        System.out.println();
	    }
	}
}

Program to print Right-angle triangle pattern in Java

print Right-angle triangle pattern

  • Take ten rows and ten columns.
  • As the rows increase column value increases. See Row 1 has 1 star, Row 2 has 2 stars.
  • Start an outer loop (row) that will iterate 10 times.
  • Under the outer loop start an inner loop (column) that iterate the same number of times
  • Inside the inner loop print star.
public class Pattern {
	public static void main(String[] args) {
	    int row, column; 
	    int row_length = 10;    
	    for (row = 1; row <= row_length; row++) {
	        for (column = 1; column <= row; column++) {
	        	System.out.print("*");
	        }
	        System.out.println();
	    }	
	}
}

Program to print Right-angle triangle with number pattern in Java

Right-angle triangle with number pattern

Similar to the previous program the logic to print a right-angle triangle with number pattern in Java is almost the same.

  • Take ten rows and ten columns.
  • As the rows increase column value increases. See Row 1 has 1 star, Row 2 has 2 stars.
  • Start an outer loop (row) that will iterate 10 times.
  • Under the outer loop start an inner loop (column) that iterate the same number of times
  • Inside the inner loop print column value.
public class Pattern {
	public static void main(String[] args) {
	    int row, column; 
	    int row_length = 10;    
	    for (row = 1; row <= row_length; row++) {
	        for (column = 1; column <= row; column++) {
	        	// you can remove %3d and use %d as well 
	        	System.out.printf("%3d", column);
	        }
	        System.out.println();
	    }	
	}
}

Program to print inverted Right-angle triangle pattern in java

print inverted Right-angle triangle pattern

Similar to the previous program, the logic to print am inverted right-angle triangle with number pattern in Java is almost the same.

  • Take ten rows and ten columns.
  • As the rows increase column value decreases. See Row 1 has 10 star, Row 2 has 9 stars.
  • Start an outer loop (row) that will iterate 10 times.
  • Under the outer loop start an inner loop (column) that iterate the row length minus current row value.
  • Inside the inner loop print star.
public class Pattern {
	public static void main(String[] args) {
	    int row, column; 
	    int row_length = 10;
	    
	    for (row = 0; row < row_length; row++) {
	        for (column = 0; column < row_length - row; column++) {
	        	System.out.print("*");
	        }
	        System.out.println();
	    }
	}
}

Program to print inverted right-angle triangle with number pattern in Java

Print inverted right-angle triangle with number pattern program in Java

Exactly same as the previous program, we just print numbers instead of stars.

  • Take ten rows and ten columns.
  • Observe, as the rows increase, column decreases.
  • Initialize a counter variable with 1.
  • Print the counter value inside inner loop.
  • After each row iteration, increment counter by 1.
public class Pattern {
	public static void main(String[] args) {
		int row, column; 
		int row_length = 10;
		int counter = 1;
		
		for (row = 0; row < row_length; row++) {
			for (column = 0; column < row_length - row ; column++) {
				System.out.printf("%d", counter);
			}
			counter++;
			System.out.println();
		}
		
	}
}

Program to print mirror Right-angle triangle pattern in java

print mirror Right-angle triangle pattern

  • Take 10 rows and 10 columns.
  • The blank spaces in the grid (marked in yellow) will come in play here.
  • Observe, as rows increase, space decreases. Row 1 has 9 spaces; Row 2 has 8 spaces.
  • Again, as rows increases column (star) value increases. Row 1 has 10 stars; Row 2 has 9 stars.
  • Start an outer loop (row) that will iterate 10 times.
  • Under the outer loop, start an inner loop (for spaces) that iterate till total row length minus current row value.
  • After the inner loop for spaces, start loop again (for star) that iterate till current
    row value. Print star inside this loop.
public class Pattern {
	public static void main(String[] args) {
		int row, column, spaces;
		int row_length = 10;

		for (row = 1; row <= row_length; row++) {
			for (spaces = 0; spaces < row_length - row; spaces++) {
				System.out.print(" ");
			}
			for (column = 1; column <= row; column++) {
				System.out.print("*");
			}
			System.out.println();
		}
	}
}

Program to print inverted mirror Right-angle triangle pattern in Java

print inverted mirror Right-angle triangle pattern

  • Take ten rows and ten columns.
  • The blank spaces in the grid (marked in yellow) will come in play here.
  • Observe, as rows increase, space increases. Row 1 has 0 space; Row 2 has 1 space.
  • Again, as rows increases column (star) value decreases. Row 1 has 10 stars; Row 2 has 9 stars.
  • Start an outer loop (row) that will iterate 10 times.
  • Under the outer loop, start an inner loop (for spaces) that iterate till current row value.
  • After the inner loop for spaces, start loop again (for star) that iterate till total row length minus current row value. Print star inside this loop.
public class Pattern {
	public static void main(String[] args) {
		int row, column, spaces; 
	    int row_length = 10;
	    
	    for (row = 0; row < row_length; row++) {
	    	for (spaces = 0; spaces <  row ; spaces++) {
	    		System.out.print(" ");
	        }
	        for (column = 0; column < row_length - row ; column++) {
	        	System.out.print("*");
	        }
	        System.out.println();
	    }
	}
}

Program to print pyramid with star pattern in java

print pyramid with star pattern

 

To print star with pyramid program in Java, logic is same as printing right angle triangle. The trick here is playing with spaces.

  • Observe, as rows increases spaces decreases (marked in yellow). Row 1 has 10 stars; Row 2 has 9 stars.
  • Again, as the rows increase column (stars) increases. See Row 1 has 1 star, Row 2 has 2 stars.
  • Start an outer loop (row) that will iterate five times.
  • Under the outer loop, start an inner loop (for spaces) that iterate till total row length minus current row value.
  • After the inner loop for spaces, start loop again (for star) that iterate till current row value.
  • Print star inside this loop
public class Pattern {
	public static void main(String[] args) {
	    int row, column, spaces; 
	    int row_length = 10;
	    
	    for (row = 1; row <= row_length; row++) {
	    	for (spaces = 0; spaces < row_length - row ; spaces++) {
	    		System.out.print(" ");
	        }
	        for (column = 1; column <= row ; column++) {
	        	System.out.print("* ");
	        }
	        System.out.println();
	    }
	}
}

Program to print pyramid with number pattern in Java

Program to print pyramid pattern in Java

With the side-by-side comparison and color coding, you can understand there are two triangles in this pyramid pattern.

  • Break the program in two section.
  • The left section (marked with green), has spaces and value. Here, number of rows is equals to number of columns. After printing the spaces use value_print to print the output and increment it by 1.
  • Adjust the value_print by decrementing it by 2.
  • The right section (marked with red), number of columns is one less than number of rows.
  • Inside the inner loop, print value_print variable and decrement it by 1.
public class Pattern {
	public static void main(String[] args) {
		int row, column1, column2, spaces;
		int row_length = 5;
		int value_print = 1;

		for (row = 1; row <= row_length; row++) {
			for (spaces = 1; spaces <= row_length - row; spaces++) {
				System.out.print(" ");
			}
			value_print = row;
			for (column1 = 1; column1 <= row; column1++) {
				System.out.printf("%d", value_print);
				value_print++;
			}
			value_print -= 2;
			for (column2 = 1; column2 < row; column2++) {
				System.out.printf("%d", value_print);
				value_print--;
			}
			System.out.println();
		}
	}
}

Program to print hollow down triangle in Java

print hollow down triangle

To print hollow down triangle / inverted pyramid, use the code below.

  • On the first row (marked with red), print star in all columns, use this condition  (row==0)
  • From second till last row (marked with green), print star in the first row and
    current column that matches current row, use this condition  (column == 0 || column == (row_length - row)-1)
  • If the condition does not satisfy, print spaces.
public class Pattern {
	public static void main(String[] args) {

		int row, column, spaces;
		int row_length = 10;

		for (row = 0; row < row_length; row++) {
			for (spaces = 0; spaces < row; spaces++) {
				System.out.print(" ");
			}
			for (column = 0; column < row_length - row; column++) {
				if (row == 0) {
					System.out.print("* ");
				} else if (column == 0 || column == (row_length - row) - 1) {
					System.out.print("* ");
				} else {
					System.out.print("  ");
				}
			}
			System.out.println();
		}

	}
}

Program to print full diamond pattern in Java

print full diamond pattern

 

To print full diamond with star pattern, use the logic below

  • The upper section (marked with green) is similar to pyramid pattern.
  • The lower section (marked with red) is similar to inverted pyramid pattern
public class Pattern {
	public static void main(String[] args) {
		int row, column, spaces; 
	    int row_length = 5;
	    
	    // upper section
	    for (row = 1; row <= row_length; row++) {
	    	for (spaces = 1; spaces <= row_length-row; spaces++) {
	    		System.out.print(" ");
	    	}
	       
	    	for (column = 1; column <= row; column++) {
	    		System.out.print("* ");
	        }
	    	
	    	System.out.println();
	    }
	    
	    // lower section
	    row_length -= 1;
	    for (row = 0; row < row_length; row++) {
	    	for (spaces = 0; spaces <= row; spaces++) {
	    		System.out.print(" ");
	    	}
	       
	    	for (column = 0; column < row_length-row; column++) {
	    		System.out.print("* ");
	        }
	    	
	    	System.out.println();
	    }
	}
}

Print hourglass/sand-glass with star pattern program in Java

hourglass/sandglass with star pattern

To print hourglass/sandglass with star pattern, use the logic below.

  • The upper section (marked with green) has the same logic as inverted pyramid.
  • The lower section (marked with red) has the same logic as pyramid pattern.
public class Pattern {
	public static void main(String[] args) {
		int row, column, spaces;
		int row_length = 5;

		// upper section
		for (row = 0; row < row_length; row++) {
			for (spaces = 0; spaces < row; spaces++) {
				System.out.print(" ");
			}
			for (column = 0; column < row_length - row; column++) {
				System.out.print("* ");
			}
			System.out.println();
		}

		// lower section
		for (row = 1; row <= row_length; row++) {
			for (spaces = 1; spaces <= row_length - row; spaces++) {
				System.out.print(" ");
			}
			for (column = 1; column <= row; column++) {
				System.out.print("* ");
			}
			System.out.println();
		}

	}
}

Program to print letter F with star pattern in Java

Java program to print letter F with star pattern

With the side by side-by-side comparison as well as color coding, understand the coding logic.

  • Take eleven rows and ten columns, for the 6th row will be the exact middle.
  • On the first and middle (marked with green), print star in all columns, use this condition (row == 1 || row == row_middle)
  • From star in first column of each (marked with red), use this condition column == 1
  • If the condition does not satisfy, print spaces.
public class Pattern {
	public static void main(String[] args) {
		int row, column;
		int row_length = 11;
		int column_length = 10;
		int row_middle = 6;

		for (row = 1; row <= row_length; row++) {
			for (column = 1; column <= column_length; column++) {
				if (row == 1 || row == row_middle) {
					System.out.print("*");
				} else if (column == 1) {
					System.out.print("*");
				} else {
					System.out.print(" ");
				}
			}
			System.out.println();
		}
	}
}

Program to print letter Z with star pattern in Java

print letter Z with star pattern

With the side by side-by-side comparison as well as color coding, understand the coding logic.

  • Take ten rows and ten columns.
  • On the first and last row (marked with green), print star descending order, use this condition (row == 1 || row == row_length)
  • From second till second-to-last row in first column of each (marked with red), use this condition column < row_length-row
  • If the condition does not satisfy, print spaces.
public class Pattern {
    public static void main(String[] args) {
        int row, column;
        int row_length = 10;
        int column_length = 10;

        for (row = 1; row <= row_length; row++) {
            for (column = 1; column <= column_length; column++) {
                if (row == 1 || row == row_length) {
                    System.out.print("*");
                } else if (column == (row_length - row) + 1) {
                    System.out.print("*");
                } else {
                    System.out.print(" ");
                }
            }
            System.out.println();
        }

    }
}

Print number two with star pattern program in Java

Print number two with star pattern program in Java

With the side by side-by-side comparison as well as color coding, I have marked the coding logic.

  • Take eleven rows and ten columns, for the 6th row will be the exact middle.
  • On the first, middle and last row (marked with green), print star in all columns, use this condition (row == 1 || row == row_middle || row == row_length)
  • From second till middle row (marked with red), print star at the last column, use this condition column == column_length && row < row_middle
  • From middle till second-to-last row (marked with blue), print star on the column, use this condition column == 1 && row > row_middle
  • If the condition does not satisfy, print spaces.
public class Pattern {
	public static void main(String[] args) {
		int row, column;
		int row_length = 11;
		int column_length = 10;
		int row_middle = 6;

		for (row = 1; row <= row_length; row++) {
			for (column = 1; column <= column_length; column++) {
				if (row == 1 || row == row_middle || row == row_length) {
					System.out.print("*");
				} else if (column == column_length && row <= row_middle) {
					System.out.print("*");
				} else if (column == 1 && row > row_middle) {
					System.out.print("*");
				} else {
					System.out.print(" ");
				}

			}
			System.out.println();
		}

	}
}

Print number three with star pattern program in Java

Print number three with star pattern

With the side by side-by-side comparison as well as color coding, I have marked the coding logic.

  • Take eleven rows and ten columns, for the 6th row will be the exact middle.
  • On the first, middle and last row (marked with green), print star in all columns, use this condition (row == 1 || row == row_middle || row == row_length)
  • From second till last row (marked with red), print star at the last column, use this condition column == column_length 
  • If the condition does not satisfy, print spaces.
public class Pattern {
    public static void main(String[] args) {
        int row, column;
        int row_length = 11;
        int column_length = 10;
        int row_middle = 6;

        for (row = 1; row <= row_length; row++) {
            for (column = 1; column <= column_length; column++) {
                if (row == 1 || row == row_middle || row == row_length) {
                    System.out.print("*");
                } else if (column == column_length) {
                    System.out.print("*");
                } else {
                    System.out.print(" ");
                }

            }
            System.out.println();
        }

    }
}

 

Program to print pascal triangle in Java

Program to print pascal triangle in Java

With the side-by-side comparison and color coding, you can understand there are two triangles in this pyramid pattern.

  • In Pascal’s triangle, each number is the sum of the two numbers directly above it. Read more here.
  • For first row and first column of each row, initialize the value as 1.
  • For the rest calculate by v = v * (row - column + 1) / column
public class Pattern {
	public static void main(String[] args) {
	    int row, column, spaces; 
	    int row_length = 6;
	    int v = 0;
	    
	    for (row = 0; row < row_length; row++) {
	    	for (spaces = 1; spaces < row_length - row ; spaces++) {
	    		System.out.printf(" ");
	        }
	        for (column = 0; column <= row ; column++) {
	        	if(column == 0 || row == 0) {
	        		v = 1;
				} else {
					v = v * (row-column + 1)/column;
				}
	        	System.out.printf("%d ",v);
	        }
	        System.out.println();
	    }

	}
}

Program to print Floyd triangle in java

print Floyd triangle

To print the Floyd triangle, use the logic below

  • Observe, at each iteration value increments by 1.
  • Use a counter that increments by 1 inside inner loop.
public class Pattern {
	public static void main(String[] args) {
	    int row, column; 
	    int row_length = 5;
	    int counter = 1;
	    
	    for (row = 1; row <= row_length; row++) {
	        for (column = 1; column <= row; column++) {
	        	System.out.printf("%d ", counter);
	        	counter++;
	        }
	        System.out.println();
	    }

	}
}

Program to print alphanumeric pattern in java

 print alphanumeric pattern

To print the alphanumeric pattern given above, use the logic mention below.

  • Observe, starting with ASCII value of A (marked in green) combining with current row number (marked in red).
  • While iterating column wise, character and number both increments by 1.
  • Print character and digit separately.
public class Pattern {
	public static void main(String[] args) {
		int row, column;
		int row_length = 5;
		int ascii_code_start = 65;
		char print_value;

		for (row = 1; row <= row_length; row++) {
			for (column = 0; column <= row_length - row; column++) {
				System.out.printf("%c", (ascii_code_start + row + column) - 1);
				System.out.printf("%d", (row + column));
				System.out.print(" ");
			}
			System.out.println();
		}
	}
}

Claim this exclusive offer now

Don’t waste your time looking for anything else. Purchase this book at the cost of less than a cup of coffee.

Buy now

Get the latest updates directly in your inbox for FREE: Subscribe here

Share your feedback / let me know your doubts regarding this tutorial in the comment box given below.

Share:
Hasan

Shahrukh Khan (Hasan)

A software engineer who's a die-hard coder, blogger, dreamer and mentor with years of expertise in web development. Know more...

Related Posts

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.