0% found this document useful (0 votes)
3 views

5Medium-Level Java Pattern Questions to Sharpen Your Logic

The document contains code examples for generating various patterns using Java, including Pascal's Triangle, Hollow Pyramid, Number Pyramid, Binary Number Triangle, and Inverted Number Pyramid. Each section provides a brief description of the output along with the corresponding Java code. The patterns demonstrate different ways to format numbers and symbols in a structured manner.

Uploaded by

nagavelli09
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

5Medium-Level Java Pattern Questions to Sharpen Your Logic

The document contains code examples for generating various patterns using Java, including Pascal's Triangle, Hollow Pyramid, Number Pyramid, Binary Number Triangle, and Inverted Number Pyramid. Each section provides a brief description of the output along with the corresponding Java code. The patterns demonstrate different ways to format numbers and symbols in a structured manner.

Uploaded by

nagavelli09
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

✅ 1.

Pascal’s Triangle
output: 1
1 1
1 2 1
1 3 3 1
1 4 6 4 1

Code:
public class PascalsTriangle {
public static void main(String[] args) {
int rows = 5;
for (int i = 0; i < rows; i++) {
int number = 1;
System.out.printf("%" + (rows - i) * 2 + "s", "");
for (int j = 0; j <= i; j++) {
System.out.printf("%4d", number);
number = number * (i - j) / (j + 1);
}
System.out.println();
}
}
}
✅ 2. Hollow Pyramid Pattern
output: *
* *
* *
* *
*********
Code: public class HollowPyramid {
public static void main(String[] args) {
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++) {
if (k == 1 || k == (2 * i - 1) || i == n)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
}
✅ 3. Number Pyramid Pattern
1
output:
2 3
4 5 6
7 8 9 10
11 12 13 14 15

Code: public class NumberPyramid {


public static void main(String[] args) {
int n = 5, num = 1;
for (int i = 1; i <= n; i++) {
for (int j = i; j < n; j++) System.out.print(" ");
for (int k = 1; k <= i; k++) {
System.out.print(num++ + " ");
}
System.out.println();
}
}
}
✅ 4. Binary Number Triangle
output: 1
01
101
0101
10101
Code:
public class BinaryTriangle {
public static void main(String[] args) {
int n = 5;
for (int i = 1; i <= n; i++) {
int num = (i % 2 == 0) ? 0 : 1;
for (int j = 1; j <= i; j++) {
System.out.print(num + " ");
num = 1 - num;
}
System.out.println();
}
}
}
✅ 5. Inverted Number Pyramid
output:
12345
2345
345
45
5

Code: public class InvertedNumberPyramid {


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

You might also like