Membuat Pola Piramid

Assalamualaikum, . .
Kali ini saya akan membagikan cara membuat pola piramid pada pemrograman java, . .

public class PyramidPattern {

public static void main(String[] iriahus) {
@SuppressWarnings("resource")
Scanner s = new Scanner(System.in);

System.out.print("Enter the number of lines (1 to 99): ");
int lines = s.nextInt();

if (lines < 1 || lines > 99) {
System.out.println("Invalid input");
System.exit(lines);
} else {
lines++;
}

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

for (int j = i; j > 0; j--) {
System.out.printf("%3d", j);
}

for (int j = 2; j <= i; j++) {
System.out.printf("%3d", j);
}

System.out.println();
}

System.out.println();

// Bonus pattern
for (int i = 1; i < lines; i++) {
for (int j = i; j < lines; j++) {
System.out.print("   ");
}

for (int j = 1; j <= i; j++) {
System.out.printf("%3d", j);
}

for (int j = 2; j <= i; j++) {
System.out.printf("%3d", (j + i - 1));
}

System.out.println();
}
}

}

Semoga bermanfaat, . .

Subscribe to receive free email updates:

0 Response to "Membuat Pola Piramid"

Post a Comment