-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPyramidPattern.java
More file actions
28 lines (23 loc) · 1 KB
/
PyramidPattern.java
File metadata and controls
28 lines (23 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public class PyramidPattern {
public static void main(String[] args) {
int maxNum = 128; // Maximum number in the pyramid
int levels = (int)(Math.log(maxNum)/Math.log(2)) + 1;
for (int i = 1; i <= levels; i++) {
int centerValue = (int) Math.pow(2, i - 1);
int spaces = levels - i;
for (int s = 0; s < spaces; s++) {
System.out.print(" "); //for each missing number
}
// Loop to print the left side and center of the pyramid level.
for (int j = 1; j <= centerValue; j *= 2) {
System.out.printf("%4d", j);
}
// Loop to print the right side of the pyramid level.
for (int j = centerValue / 2; j >= 1; j /= 2) {
System.out.printf("%4d", j);
}
// Move to the next line for the next level of the pyramid.
System.out.println();
}
}
}