Top 20 Java Pattern Programs Asked in Interviews Questions (With Code using for loop & Output)

Top 20 Java Pattern Questions Asked in Interviews

Top 20 Java Pattern Questions Asked in Interviews

Here are 20 frequently asked Java pattern program questions with their code and expected output. Use them for coding rounds or practice.


1. Right-Angled Triangle

* * * * * * * * * * * * * * *
for(int i = 1; i <= 5; i++) {
    for(int j = 1; j <= i; j++) {
        System.out.print("* ");
    }
    System.out.println();
}

2. Inverted Right-Angled Triangle

* * * * * * * * * * * * * * *
for(int i = 5; i >= 1; i--) {
    for(int j = 1; j <= i; j++) {
        System.out.print("* ");
    }
    System.out.println();
}

3. Pyramid Pattern

* *** ***** ******* *********
for(int i = 1; i <= 5; i++) {
    for(int j = i; j < 5; j++) System.out.print(" ");
    for(int k = 1; k <= (2 * i - 1); k++) System.out.print("*");
    System.out.println();
}

4. Diamond Pattern

* *** ***** ******* ********* ******* ***** *** *
int n = 5;
for(int i = 1; i <= n; i++) {
    for(int j = i; j < n; j++) System.out.print(" ");
    for(int k = 1; k <= (2 * i - 1); k++) System.out.print("*");
    System.out.println();
}
for(int i = n - 1; i >= 1; i--) {
    for(int j = n; j > i; j--) System.out.print(" ");
    for(int k = 1; k <= (2 * i - 1); k++) System.out.print("*");
    System.out.println();
}

5. Square Pattern

* * * * * * * * * * * * * * * * * * * * * * * * *
for(int i = 1; i <= 5; i++) {
    for(int j = 1; j <= 5; j++) {
        System.out.print("* ");
    }
    System.out.println();
}

6. Hollow Square

* * * * * * * * * * * * * * * *
for(int i = 1; i <= 5; i++) {
    for(int j = 1; j <= 5; j++) {
        if(i == 1 || i == 5 || j == 1 || j == 5) System.out.print("* ");
        else System.out.print("  ");
    }
    System.out.println();
}

7. Number Triangle

1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
for(int i = 1; i <= 5; i++) {
    for(int j = 1; j <= i; j++) {
        System.out.print(j + " ");
    }
    System.out.println();
}

8. Inverted Number Triangle

1 2 3 4 5 1 2 3 4 1 2 3 1 2 1
for(int i = 5; i >= 1; i--) {
    for(int j = 1; j <= i; j++) {
        System.out.print(j + " ");
    }
    System.out.println();
}

9. Floyd’s Triangle

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
int num = 1;
for(int i = 1; i <= 5; i++) {
    for(int j = 1; j <= i; j++) {
        System.out.print(num++ + " ");
    }
    System.out.println();
}

10. 0-1 Triangle

1 0 1 1 0 1 0 1 0 1 1 0 1 0 1
for(int i = 1; i <= 5; i++) {
    for(int j = 1; j <= i; j++) {
        System.out.print((i + j) % 2 + " ");
    }
    System.out.println();
}

11. Pascal’s Triangle

1 1 1 1 2 1 1 3 3 1 1 4 6 4 1
for(int i = 0; i < 5; i++) {
    int number = 1;
    for(int j = 0; j < 5 - i; j++)
        System.out.print(" ");
    for(int j = 0; j <= i; j++) {
        System.out.print(number + " ");
        number = number * (i - j) / (j + 1);
    }
    System.out.println();
}

12. Butterfly Pattern

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
int n = 4;
for(int i = 1; i <= n; i++) {
    for(int j = 1; j <= i; j++) System.out.print("* ");
    for(int j = 1; j <= 2*(n-i); j++) System.out.print("  ");
    for(int j = 1; j <= i; j++) System.out.print("* ");
    System.out.println();
}
for(int i = n; i >= 1; i--) {
    for(int j = 1; j <= i; j++) System.out.print("* ");
    for(int j = 1; j <= 2*(n-i); j++) System.out.print("  ");
    for(int j = 1; j <= i; j++) System.out.print("* ");
    System.out.println();
}

13. Hollow Pyramid

* * * * * * * *********
int n = 5;
for(int i = 1; i <= n; i++) {
    for(int j = i; j < n; j++) System.out.print(" ");
    for(int j = 1; j <= (2 * i - 1); j++) {
        if(j == 1 || j == (2 * i - 1) || i == n) System.out.print("*");
        else System.out.print(" ");
    }
    System.out.println();
}

14. Cross Pattern

* * * * * * * * *
int n = 5;
for(int i = 1; i <= n; i++) {
    for(int j = 1; j <= n; j++) {
        if(j == i || j == (n - i + 1)) System.out.print("*");
        else System.out.print(" ");
    }
    System.out.println();
}

15. Right-Angled Number Triangle (Continuous)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
int num = 1;
for(int i = 1; i <= 5; i++) {
    for(int j = 1; j <= i; j++) {
        System.out.print(num++ + " ");
    }
    System.out.println();
}

16. Alphabet Triangle

A A B A B C A B C D A B C D E
for(int i = 1; i <= 5; i++) {
    for(char c = 'A'; c < 'A' + i; c++) {
        System.out.print(c + " ");
    }
    System.out.println();
}

17. Reverse Alphabet Triangle

E D C B A D C B A C B A B A A
for(int i = 5; i >= 1; i--) {
    for(char c = (char)('A' + i - 1); c >= 'A'; c--) {
        System.out.print(c + " ");
    }
    System.out.println();
}

18. Zig-Zag Star Pattern

* * * * * * * * *
int n = 9;
for(int i = 1; i <= 3; i++) {
    for(int j = 1; j <= n; j++) {
        if((i + j) % 4 == 0 || (i == 2 && j % 4 == 0)) System.out.print("*");
        else System.out.print(" ");
    }
    System.out.println();
}

19. Hollow Diamond

* * * * * * * * * * * * * * * *
int n = 5;
for(int i = 1; i <= n; i++) {
    for(int j = 1; j <= n - 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();
}
for(int i = n - 1; i >= 1; i--) {
    for(int j = 1; j <= n - 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();
}

20. Binary Triangle Pattern

1 0 1 1 0 1 0 1 0 1 1 0 1 0 1
for(int i = 1; i <= 5; i++) {
    for(int j = 1; j <= i; j++) {
        System.out.print(((i + j) % 2) + " ");
    }
    System.out.println();
}

🔁 Tip: Practice these patterns regularly and also dry-run the loops on paper to fully understand flow control. Bookmark this page for your coding interview prep!

📌 Note: More advanced pattern questions (like Pascal’s Triangle, Butterfly Pattern, and Zigzag) will be covered in part 2.

Post a Comment

0 Comments