Skip to content

Instantly share code, notes, and snippets.

@ad3n
Last active July 17, 2016 15:16
Show Gist options
  • Save ad3n/d10abb60840d284698c443337b084534 to your computer and use it in GitHub Desktop.
Save ad3n/d10abb60840d284698c443337b084534 to your computer and use it in GitHub Desktop.
Print segitiga Angka bolak balik
<?php
class Segitiga
{
private $limit;
public function setLimit($limit)
{
$this->limit = $limit;
}
public function printSegitigaAtas()
{
for ($i = 1; $i < $this->limit; $i++) {
for ($j = ($this->limit - $i); $j >= 0; $j--) {
echo ' ';
}
$flag = true;
$temp = $i;
while ($flag) {
if (0 !== $temp && -1 !== $temp) {
echo abs($temp);
}
$temp--;
if ($temp + 1 === -1 * $i) {
$flag = false;
}
}
echo PHP_EOL;
}
}
public function printSegitigaBawah()
{
for ($i = $this->limit; $i > 0; $i--) {
for ($j = ($this->limit - $i); $j >= 0; $j--) {
echo ' ';
}
$flag = true;
$temp = $i;
while ($flag) {
if (0 !== $temp && -1 !== $temp) {
echo abs($temp);
}
$temp--;
if ($temp + 1 === -1 * $i) {
$flag = false;
}
}
echo PHP_EOL;
}
}
}
$segitiga = new Segitiga();
$segitiga->setLimit(7);
$segitiga->printSegitigaAtas();
$segitiga->printSegitigaBawah();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment