Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*********************************
- FILENAME : diamond.php
- PURPOSE : print square number in diamond pattern
- CREATED DATE : 2012-12-13
- CREATED BY : cahya dsn
- **********************************/
- function diamond($n=3)
- {
- $space=strlen($n*$n)+1;
- for($i=0;$i<$n*2;$i++){
- $rst='';
- if($i<$n){
- for($j=0;$j<=$i;$j++){
- $r=($n*$i+1)-($n-1)*$j;
- $rst.=str_pad($r, $space, " ", STR_PAD_BOTH);
- }
- }else{
- for($j=2*$n-$i-1;$j>0;$j--){
- $r=($j+$i+1)*$n-($n*$n+$j)+1;
- $rst.=str_pad($r, $space, " ", STR_PAD_BOTH);
- }
- }
- echo str_pad($rst, $n*$space, " ", STR_PAD_BOTH)."\n";
- }
- }
- //example use:
- echo "<pre>";
- diamond(3);
- diamond(6);
- diamond(10);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement