Top Pattern Printing Programs in Java and JavaScript - 15 Questions With Solutions
Head First Java, Third Edition
Ready to learn Java? This book combines puzzles, strong visuals, mysteries, and soul-searching interviews with famous Java objects to engage you in many different ways.
Pattern printing questions are a staple of programming interviews and college placements โ especially in India. They test your ability to think in terms of rows and columns, understand nested loops, and translate a visual pattern into working logic. They are also one of the most effective ways to build loop intuition early in your programming journey.
In this guide we cover the most commonly asked pattern printing questions with complete solutions in both Java and JavaScript, step-by-step explanations of the logic behind each pattern, and the core mental model that makes all pattern problems easier to solve.
The Core Mental Model for All Pattern Problems
Before jumping into patterns, internalize this one framework โ it applies to every pattern problem you will ever see:
// Every pattern is a grid โ rows ร columns
// Outer loop = rows (top to bottom)
// Inner loop = columns (left to right)
// At each (row, col) position โ decide what to print
// Ask yourself three questions for any pattern:
// 1. How many rows does the pattern have? โ outer loop range
// 2. How many columns per row? โ inner loop range
// 3. What condition determines what to print? โ if/else inside inner loop
// Template for most patterns:
for (let row = 1; row <= n; row++) {
let line = "";
for (let col = 1; col <= row; col++) { // or some other condition
line += "* "; // or a number, or a space
}
console.log(line);
}
Pattern 1 โ Right Triangle Star Pattern
/*
*
* *
* * *
* * * *
* * * * *
*/
// JavaScript
function rightTriangle(n) {
for (let row = 1; row <= n; row++) {
let line = "";
for (let col = 1; col <= row; col++) {
line += "* ";
}
console.log(line);
}
}
rightTriangle(5);
// Java
public static void rightTriangle(int n) {
for (int row = 1; row <= n; row++) {
StringBuilder line = new StringBuilder();
for (int col = 1; col <= row; col++) {
line.append("* ");
}
System.out.println(line);
}
}
Logic: Row 1 prints 1 star, row 2 prints 2 stars, row N prints N stars. The inner loop runs from 1 to the current row number.
Pattern 2 โ Inverted Right Triangle
/*
* * * * *
* * * *
* * *
* *
*
*/
// JavaScript
function invertedTriangle(n) {
for (let row = n; row >= 1; row--) {
let line = "";
for (let col = 1; col <= row; col++) {
line += "* ";
}
console.log(line);
}
}
invertedTriangle(5);
// Java
public static void invertedTriangle(int n) {
for (int row = n; row >= 1; row--) {
StringBuilder line = new StringBuilder();
for (int col = 1; col <= row; col++) {
line.append("* ");
}
System.out.println(line);
}
}
Logic: Start the outer loop from N and count down to 1. Same inner loop as Pattern 1 โ runs up to current row value.
Pattern 3 โ Full Pyramid
/*
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
*/
// JavaScript
function pyramid(n) {
for (let row = 1; row <= n; row++) {
let spaces = "";
let stars = "";
// Leading spaces = (n - row) spaces
for (let s = 1; s <= n - row; s++) {
spaces += " ";
}
// Stars per row = (2 * row - 1)
for (let col = 1; col <= 2 * row - 1; col++) {
stars += "* ";
}
console.log(spaces + stars);
}
}
pyramid(5);
// Java
public static void pyramid(int n) {
for (int row = 1; row <= n; row++) {
StringBuilder line = new StringBuilder();
for (int s = 1; s <= n - row; s++) {
line.append(" ");
}
for (int col = 1; col <= 2 * row - 1; col++) {
line.append("* ");
}
System.out.println(line);
}
}
Logic: Row 1 has 1 star, row 2 has 3, row 3 has 5 โ
each row has (2 * row - 1) stars. Leading spaces =
(n - row) to center the pyramid.
Pattern 4 โ Inverted Pyramid
/*
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
*/
// JavaScript
function invertedPyramid(n) {
for (let row = n; row >= 1; row--) {
let spaces = "";
let stars = "";
for (let s = 1; s <= n - row; s++) {
spaces += " ";
}
for (let col = 1; col <= 2 * row - 1; col++) {
stars += "* ";
}
console.log(spaces + stars);
}
}
invertedPyramid(5);
Pattern 5 โ Diamond Pattern
/*
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
*/
// JavaScript โ combine pyramid + inverted pyramid
function diamond(n) {
// Upper half (including middle)
for (let row = 1; row <= n; row++) {
let line = " ".repeat(n - row) + "* ".repeat(2 * row - 1);
console.log(line);
}
// Lower half (excluding middle)
for (let row = n - 1; row >= 1; row--) {
let line = " ".repeat(n - row) + "* ".repeat(2 * row - 1);
console.log(line);
}
}
diamond(5);
// Java
public static void diamond(int n) {
// Upper half
for (int row = 1; row <= n; row++) {
for (int s = 1; s <= n - row; s++) System.out.print(" ");
for (int col = 1; col <= 2 * row - 1; col++) System.out.print("* ");
System.out.println();
}
// Lower half
for (int row = n - 1; row >= 1; row--) {
for (int s = 1; s <= n - row; s++) System.out.print(" ");
for (int col = 1; col <= 2 * row - 1; col++) System.out.print("* ");
System.out.println();
}
}
Pattern 6 โ Number Triangle
/*
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
*/
// JavaScript
function numberTriangle(n) {
for (let row = 1; row <= n; row++) {
let line = "";
for (let col = 1; col <= row; col++) {
line += col + " ";
}
console.log(line.trim());
}
}
numberTriangle(5);
// Java
public static void numberTriangle(int n) {
for (int row = 1; row <= n; row++) {
for (int col = 1; col <= row; col++) {
System.out.print(col + " ");
}
System.out.println();
}
}
Pattern 7 โ Same Number Each Row (Floyd's Triangle Variant)
/*
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
*/
// JavaScript
function sameNumberRow(n) {
for (let row = 1; row <= n; row++) {
let line = "";
for (let col = 1; col <= row; col++) {
line += row + " "; // print row number, not col number
}
console.log(line.trim());
}
}
sameNumberRow(5);
Pattern 8 โ Floyd's Triangle
/*
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
*/
// JavaScript โ continuous number counter
function floydsTriangle(n) {
let num = 1;
for (let row = 1; row <= n; row++) {
let line = "";
for (let col = 1; col <= row; col++) {
line += num + " ";
num++;
}
console.log(line.trim());
}
}
floydsTriangle(5);
// Java
public static void floydsTriangle(int n) {
int num = 1;
for (int row = 1; row <= n; row++) {
for (int col = 1; col <= row; col++) {
System.out.print(num++ + " ");
}
System.out.println();
}
}
Logic: Maintain a separate counter num
that starts at 1 and increments with every star printed โ independent
of row and column numbers.
Pattern 9 โ Alphabet Triangle
/*
A
A B
A B C
A B C D
A B C D E
*/
// JavaScript
function alphabetTriangle(n) {
for (let row = 1; row <= n; row++) {
let line = "";
for (let col = 1; col <= row; col++) {
line += String.fromCharCode(64 + col) + " "; // 65=A, 66=B...
}
console.log(line.trim());
}
}
alphabetTriangle(5);
// Java
public static void alphabetTriangle(int n) {
for (int row = 1; row <= n; row++) {
for (int col = 1; col <= row; col++) {
System.out.print((char)('A' + col - 1) + " ");
}
System.out.println();
}
}
Pattern 10 โ Same Alphabet Each Row
/*
A
B B
C C C
D D D D
E E E E E
*/
// JavaScript
function sameAlphabetRow(n) {
for (let row = 1; row <= n; row++) {
let line = "";
const char = String.fromCharCode(64 + row); // A, B, C...
for (let col = 1; col <= row; col++) {
line += char + " ";
}
console.log(line.trim());
}
}
sameAlphabetRow(5);
Pattern 11 โ Hollow Square
/*
* * * * *
* *
* *
* *
* * * * *
*/
// JavaScript
function hollowSquare(n) {
for (let row = 1; row <= n; row++) {
let line = "";
for (let col = 1; col <= n; col++) {
// Print star on border, space inside
if (row === 1 || row === n || col === 1 || col === n) {
line += "* ";
} else {
line += " ";
}
}
console.log(line.trim());
}
}
hollowSquare(5);
// Java
public static void hollowSquare(int n) {
for (int row = 1; row <= n; row++) {
for (int col = 1; col <= n; col++) {
if (row == 1 || row == n || col == 1 || col == n) {
System.out.print("* ");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
Logic: Print a star only when we are on the first row, last row, first column, or last column. Everything else is a space.
Pattern 12 โ Hollow Triangle
/*
*
* *
* *
* *
* * * * *
*/
// JavaScript
function hollowTriangle(n) {
for (let row = 1; row <= n; row++) {
let line = "";
for (let col = 1; col <= row; col++) {
// First col, last col of this row, or last row
if (col === 1 || col === row || row === n) {
line += "* ";
} else {
line += " ";
}
}
console.log(line.trim());
}
}
hollowTriangle(5);
Pattern 13 โ Butterfly Pattern
/*
* *
* * * *
* * * * *
* * * *
* *
*/
// JavaScript
function butterfly(n) {
// Upper half
for (let row = 1; row <= n; row++) {
let line = "";
// Left stars
for (let col = 1; col <= row; col++) line += "* ";
// Middle spaces
for (let col = 1; col <= 2 * (n - row); col++) line += " ";
// Right stars
for (let col = 1; col <= row; col++) line += "* ";
console.log(line.trim());
}
// Lower half
for (let row = n - 1; row >= 1; row--) {
let line = "";
for (let col = 1; col <= row; col++) line += "* ";
for (let col = 1; col <= 2 * (n - row); col++) line += " ";
for (let col = 1; col <= row; col++) line += "* ";
console.log(line.trim());
}
}
butterfly(5);
// Java
public static void butterfly(int n) {
// Upper half
for (int row = 1; row <= n; row++) {
for (int col = 1; col <= row; col++) System.out.print("* ");
for (int col = 1; col <= 2 * (n - row); col++) System.out.print(" ");
for (int col = 1; col <= row; col++) System.out.print("* ");
System.out.println();
}
// Lower half
for (int row = n - 1; row >= 1; row--) {
for (int col = 1; col <= row; col++) System.out.print("* ");
for (int col = 1; col <= 2 * (n - row); col++) System.out.print(" ");
for (int col = 1; col <= row; col++) System.out.print("* ");
System.out.println();
}
}
Pattern 14 โ Pascal's Triangle
/*
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
*/
// JavaScript โ using combination formula C(row, col)
function pascalsTriangle(n) {
for (let row = 0; row < n; row++) {
let line = " ".repeat(n - row - 1); // leading spaces
let value = 1;
for (let col = 0; col <= row; col++) {
line += value + " ";
value = value * (row - col) / (col + 1); // next combination value
}
console.log(line);
}
}
pascalsTriangle(5);
// Java โ cleaner with 2D array
public static void pascalsTriangle(int n) {
int[][] triangle = new int[n][n];
for (int row = 0; row < n; row++) {
triangle[row][0] = 1; // first element always 1
for (int col = 1; col <= row; col++) {
// each element = sum of two elements above it
triangle[row][col] = triangle[row-1][col-1] + triangle[row-1][col];
}
}
// Print with spacing
for (int row = 0; row < n; row++) {
System.out.print(" ".repeat(n - row));
for (int col = 0; col <= row; col++) {
System.out.printf("%-4d", triangle[row][col]);
}
System.out.println();
}
}
Logic: Each element in Pascal's Triangle equals the sum of the two elements directly above it. Row 0 and every first/last element is 1. The values correspond to binomial coefficients.
Pattern 15 โ Sandglass / Hourglass Pattern
/*
* * * * *
* * *
*
* * *
* * * * *
*/
// JavaScript
function sandglass(n) {
// Upper half โ decreasing
for (let row = 0; row < n; row++) {
let line = " ".repeat(row) + "* ".repeat(n - row);
console.log(line.trim());
}
// Lower half โ increasing (skip the middle row already printed)
for (let row = n - 2; row >= 0; row--) {
let line = " ".repeat(row) + "* ".repeat(n - row);
console.log(line.trim());
}
}
sandglass(5);
Common Mistakes in Pattern Questions
-
Off-by-one errors โ using
<instead of<=in the loop condition. Always trace your pattern manually forn = 3before submitting. - Wrong loop variable for inner loop โ the inner loop should iterate based on the current row, not always from 1 to N.
-
Not printing a newline after each row โ in Java always
call
System.out.println()at the end of each row. In JavaScript useconsole.log(line)notconsole.log(line + " "). - Printing extra spaces at line end โ trim your output string or carefully control when spaces are added. Interviewers sometimes check for trailing spaces.
-
Using print instead of println in Java โ
System.out.printdoes not add newline. UseSystem.out.println()after each row explicitly.
How to Approach Any New Pattern in an Interview
-
Count the rows โ how many rows does the pattern have
for
n = 5? That is your outer loop range. - Analyze each row โ for row 1, row 2, row 3 โ how many characters, what characters, what relationship between row number and character count?
-
Find the formula โ stars in row
i= ? spaces before rowi= ? Write the formula before coding. -
Trace manually first โ for
n = 3, trace your loops on paper. Verify output matches the pattern. - Code it โ once the logic is clear, coding is fast. The thinking comes first, always.
Pattern Types Summary
| Pattern | Inner Loop Condition | Key Logic |
|---|---|---|
| Right Triangle | col <= row |
Stars = row number |
| Inverted Triangle | col <= row |
Outer loop counts down |
| Pyramid | col <= 2*row-1 |
Spaces + odd stars |
| Diamond | Pyramid + Inverted | Two pyramids joined |
| Hollow Square | col <= n |
Border condition |
| Floyd's Triangle | col <= row |
External counter |
| Pascal's Triangle | col <= row |
Combination formula |
| Butterfly | Left + space + right | Three sections per row |
Final Thought
Pattern printing questions are not just about getting the output right โ they test how methodically you think through a problem. The ability to look at a visual pattern, extract the mathematical relationship between row, column, and content, and translate that into clean nested loops is a fundamental programming skill that transfers directly to grid problems, matrix manipulation, and two-dimensional array challenges in real interviews.
Master the core mental model โ outer loop for rows, inner loop for columns, condition for what to print โ and you will be able to solve any pattern you encounter in an interview, even one you have never seen before.
Once you are comfortable with patterns, level up your interview preparation with our coding problem guides:
Head First Java, Third Edition
Ready to learn Java? This book combines puzzles, strong visuals, mysteries, and soul-searching interviews with famous Java objects to engage you in many different ways.
๐ You Might Also Like
- โ Best Google AdSense Alternative 2026 - Monetag Review for Publishers miscellaneous
- โ Top Java Interview Questions for 6+ Years Experience (2026) java
- โ How to Start Freelancing as a Web Developer in 2026 (Complete Beginner's Guide) miscellaneous
- โ HTTP QUERY Method (RFC 10008) โ The New HTTP Method Every Developer Should Know miscellaneous
- โ MyLync - Best Free Linktree Alternative for Developers, Creators & Businesses miscellaneous