antonyjohn Posted August 10, 2007 Share Posted August 10, 2007 hai friends, i have a problem...i need to write a php code which prints an output like this 1 12 123 1234 123 12 1 Link to comment https://forums.phpfreaks.com/topic/64207-plzzzzzz-help-in-formatted-output/ Share on other sites More sharing options...
dbo Posted August 10, 2007 Share Posted August 10, 2007 Sounds like a homework assignment to me. Link to comment https://forums.phpfreaks.com/topic/64207-plzzzzzz-help-in-formatted-output/#findComment-320074 Share on other sites More sharing options...
dbo Posted August 10, 2007 Share Posted August 10, 2007 There is undoubtedly a prettier solution than this. But here you go. Modify 5 to make it grow bigger and stuff. Probably better idea to put it in a variable. Enjoy. $count_up = true; $i = 1; while($i<5 && $i!=0) { $j = 0; while($j<$i) { echo ($j+1); ++$j; } echo "<br />"; if( $count_up ) { if(($i+1) != 5) { ++$i; } else { --$i; $count_up = false; } } else { --$i; } } Link to comment https://forums.phpfreaks.com/topic/64207-plzzzzzz-help-in-formatted-output/#findComment-320089 Share on other sites More sharing options...
jitesh Posted August 10, 2007 Share Posted August 10, 2007 <?php $start = 1; $end = 4; $status = "forward"; for($i=$start;$i<=$end*2-1;$i++){ if($status == "forward"){ $to = $i; }elseif($status == "backword"){ $to = $to - 1; } for($j=1;$j<=$to;$j++){ echo $j; } echo "<br>"; if($i == $end){ $status = "backword"; } } ?> Link to comment https://forums.phpfreaks.com/topic/64207-plzzzzzz-help-in-formatted-output/#findComment-320098 Share on other sites More sharing options...
clearstatcache Posted August 10, 2007 Share Posted August 10, 2007 $max = 20; for($x=1; $x<=$max; $x++) { for($y=1; $y<=$x; $y++) { print $y; } print "\n"; } for($x=$max-1; $x>=1; $x--) { for($y=1; $y<=$x; $y++) { print $y; } print "\n"; } Link to comment https://forums.phpfreaks.com/topic/64207-plzzzzzz-help-in-formatted-output/#findComment-320162 Share on other sites More sharing options...
jitesh Posted August 10, 2007 Share Posted August 10, 2007 New Modified <?php $start = 1; $end = 4; $main_loop = $start + ($end-$start)*2+1; $status = "forward"; for($i=$start;$i<=$main_loop;$i++){ if($status == "forward"){ $to = $i; }elseif($status == "backword"){ $to = $to - 1; } for($j=$start;$j<=$to;$j++){ echo $j; } echo "<br>"; if($i == $end){ $status = "backword"; } } ?> Link to comment https://forums.phpfreaks.com/topic/64207-plzzzzzz-help-in-formatted-output/#findComment-320170 Share on other sites More sharing options...
jitesh Posted August 10, 2007 Share Posted August 10, 2007 And this is optimised <?php $start = 1; $end = 4; $main_loop = $start + ($end-$start)*2+1; for($i=$start;$i<=$main_loop;$i++){ ($i > $end) ?$to = $to - 1 : $to = $i; for($j=$start;$j<=$to;$j++){ echo $j; } echo "<br>"; } ?> Link to comment https://forums.phpfreaks.com/topic/64207-plzzzzzz-help-in-formatted-output/#findComment-320189 Share on other sites More sharing options...
gurroa Posted August 10, 2007 Share Posted August 10, 2007 while( ($dest = 4) && !isset($b) && (($b = ($max = $dest * ($i = $j = 1) + 1) < 0) == false) ) while ((!$b && $i < ($max + ($j = 0))) || ($b && ($i-- > 0 + ($j = 0))) ) while( (($j < $i ? ++$j : $max + (!$b ? ++$i : (5 + $i)) ) <= $max && print($j)) || !print("<br />".(($b = $b || ($i == $max)) ? (($i = ($i == $max ? $i - 1 : $i)) ? '' : '') : '')) ) ; Link to comment https://forums.phpfreaks.com/topic/64207-plzzzzzz-help-in-formatted-output/#findComment-320191 Share on other sites More sharing options...
dbo Posted August 10, 2007 Share Posted August 10, 2007 lmao. I'm pretty sure that the dude quit looking at this last night when the first couple of us answered his homework question for him Link to comment https://forums.phpfreaks.com/topic/64207-plzzzzzz-help-in-formatted-output/#findComment-320548 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.