-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberPyramid.java
More file actions
27 lines (23 loc) · 836 Bytes
/
NumberPyramid.java
File metadata and controls
27 lines (23 loc) · 836 Bytes
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
import java.util.Scanner;
public class NumberPyramid {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of levels: ");
int l = scanner.nextInt();
scanner.close();
for (int i = 1; i <= l; i++) {
// Print leading spaces
for (int j = 0; j < (l - i) * (l - i - 1) / 2; j++) {
System.out.print(" ");
}
// Print numbers with spaces in between
for (int j = 1; j <= i; j++) {
System.out.print(j);
if (j < i) {
System.out.print(" "); // Extra spaces between numbers
}
}
System.out.println(); // Move to next line
}
}
}