Looping 2: Diamond

Ini soal yang kedua. Soal yang pertama bisa dilihat di sini.

Hasil tampilan yang diinginkan adalah sebagai berikut:

looping: diamond

Dan berikut kode sumbernya:

<?php
$star = '*';
$line = '-';

for ($j = 8; $j >= 2; $j--) {
    for ($k = 2; $k <= $j; $k++) {
        echo $line;
    }
    for ($l = $j; $l <= 8; $l++) {
        echo $star;
    }
    for ($l = $j; $l <= 8; $l++) {
        echo $star;
    }
    for ($k = 2; $k <= $j; $k++) {
        echo $line;
    }
    echo "n";
}
for ($j = 8; $j >= 2; $j--) {
    for ($l = $j; $l <= 8; $l++) {
        echo $line;
    }
    for ($k = 2; $k <= $j; $k++) {
        echo $star;
    }
    for ($k = 2; $k <= $j; $k++) {
        echo $star;
    }
    for ($l = $j; $l <= 8; $l++) {
        echo $line;
    }
    echo "n";
}
?>

Masih ada satu soal lagi. Lihat di postingan berikutnya, ya 😉

Published by Eric Gunawan

Happiness Engineer. WordPress Ambassador. Remote Worker. Soccer News Follower. Movie Lover. Proud Father. Lucky Husband.

One thought on “Looping 2: Diamond

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: