Last active
July 17, 2016 15:16
-
-
Save ad3n/d10abb60840d284698c443337b084534 to your computer and use it in GitHub Desktop.
Print segitiga Angka bolak balik
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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