How to loop with sample input = 5
and the output :
1 2 3 4 5
0 2 3 4 5
0 0 3 4 5
0 0 0 4 5
0 0 0 0 5
PHP:
<?php
$i=5;
for($a=1; $a<=$i; $a++){
echo $a." ";
}
echo "\n";
for($a=0; $a<=$i; $a++){
if($a==1){
continue;
}
print "$a ";
}
echo "\n"; $ex = array(1,2);
for($a=1; $a<=$i; $a++){
if(in_array($a, $ex)){
continue;
}
print "$a ";
}
?>
How to solve this issue?
Using built-in functions, it's easier to read and understand:
$input = 5;
$nums = range(1, $input);
for ($zeros_count = 0; $zeros_count < $input; $zeros_count++) {
echo str_repeat('0 ', $zeros_count);
echo implode(' ', array_slice($nums, $zeros_count)) . PHP_EOL;
}
Think simple
<?php
$input = 5;
for($i = 1; $i <= $input; $i++ ) {
for($j = 1; $j <= $input; $j++) {
if( $i > $j) {
echo "0 ";
} else {
echo $j . " ";
}
}
echo "<br>";
}
?>
Related
I want to print number from 1 to 12 in matrix form and expected output is:
1 5 9
2 6 10
3 7 11
4 8 12
code:
<?php
for ($i=1; $i<=12; $i++)
{
for($j=1;$j<=$i;$j++)
{
echo $i." ";
}
echo "<br/>";
}
?>
I have got wrong output. So, How can I get expected output as I mention above? Please help me.
Thank You
Some "magic" code):
foreach (range(1,4) as $num) {
echo implode(' ', range($num,12,4)) . '<br />';
}
Version with for:
for ($i = 1; $i <= 4; $i++) {
for ($j = $i; $j <= 12; $j +=4) {
echo $j . ' ';
}
echo '<br />';
}
$z=0;
for ($x = 1; $x <= 4; $x++)
{
echo " $x ";
$z=$x;
for ($y = 1; $y <= 2; $y++)
{
$z=$z+4;
echo " $z ";
}
echo "\n";
}
$maxRow = 4;
$maxColumn = 3;
for ($row = 1; $row <= $maxRow; $row++)
{
for ($column = 1; $column <= $maxColumn; $column++) {
$number = $row + 4*($column-1);
echo $number." ";
}
echo "<br/>";
}
should work
foreach (range(1, 4) as $res) {
echo implode(' ', range($res, 12, 4));
echo "<br>";
}
Hey all i can not solve this,that's why i post it here .i am a php larner and trying solve small php problems.my problems are below.
0
1 0 1
2 1 0 1 2
3 2 1 0 1 2 3
4 3 2 1 0 1 2 3 4
how to print the following pattern ?
image link-http://imgur.com/4Y5L8ZZ
This worked. I'm not sure whether it's the best approach.
for ($i = 0; $i < 5; $i++) {
for ($a = $i; $a > 0; $a--) {
echo $a;
}
for ($b = 0; $b <= $i; $b++) {
echo $b;
}
echo "\r\n";
}
<?php
for ($out = 0; $out < 5; $out++) {
for ($row = $out; $row > 0; $row--) {
echo $row . " ";
}
for ($col = 0; $col <= $out; $col++) {
echo $col. " ";
}
echo "<br>";
}
?>
Please try executing following code snippet as a solution according to above mentioned description.
$rows=5;
for($i=0;$i<$rows;$i++)
{
echo "<br>";
for($j=$i;$j>=0;$j--)
{
echo "\t".$j;
}
for($k=1;$k<=$i;$k++)
{
echo "\t".$k;
}
}
My sample desired output should be
1 2 3 4 5
2 4
3 3
4 2
5 4 3 2 1
Here is my PHP code
for($i=1;$i <= 5;$i++) {
for($j=1;$j<=$i;$j++) {
echo "$j";
}
for($y=0;$y<(5-$i)*4;$y++) {
echo ' ';
}
for($l=$i;$l>0;$l--) {
echo "$l";
}
echo "<br/>";
}
But I got this output.
output:-
1 1
12 21
123 321
1234 4321
1234554321
Please try to solve my problem. Thanks in advance.
for($i=1; $i<=5; $i++){
echo $i." ";
}
echo "<br />";
for($i=2; $i<=5; $i++){
if($i==5){
echo $i;
}
else{
echo $i." ";
if($i==2){
echo (4)."<br />";
}
if($i==3){
echo (3)."<br />";
}
if($i==4){
echo (2)."<br />";
}
}
}
echo " ";
for($i=4; $i>=1; $i--){
echo $i." ";
}
#Mark has the best solution, I guess.
Here's a quick solution for an arbitrary array of 1-character values:
$values = range(1,7);
$count = count($values);
foreach($values as $k=>$v) {
if($k == 0)
echo implode(" ", $values), "\n";
elseif($k == $count-1)
echo implode(" ", array_reverse($values)), "\n";
else
echo $v, " ", str_repeat(" ", $count-2), $values[$count-1-$k], "\n";
}
This will produce:
1 2 3 4 5 6 7
2 6
3 5
4 4
5 3
6 2
7 6 5 4 3 2 1
$count = 5;
$last = 0;
for ($i = 1; $i <= $count; $i++) {
if($i == 1) {
for ($x = 1; $x <= 5; $x++) {
echo $x . ' ';
}
$last = $x;
} elseif ($i == 5) {
for ($b = 5; $b >= 1; $b--) {
echo $b . ' ';
}
} else{
for($c=1; $c <= 5; $c++) {
if($c == 1) {
echo $i . ' ';
} elseif ($c == 5) {
echo ($last - $i) . ' ' ;
} else {
echo ' ';
}
}
}
echo '<br>';
}
I want to print integer in triangle form which look like this
1
121
12321
I tried this but I do not get the actual result
for($i=1;$i<=3;$i++)
{
for($j=3;$j>=$i;$j--)
{
echo " ";
}
for($k=1;$k<=$i;$k++)
{
echo $k;
}
if($i>1)
{
for($m=$i; $m>=1; $m--)
{
echo $m;
}
}
echo "<br>";
}
Output of this code is:
1
1221
123321
Where am I going wrong, please guide me.
Another integer solution:
$n = 9;
print str_pad ("✭",$n," ",STR_PAD_LEFT) . PHP_EOL;
for ($i=0; $i<$n; $i++){
print str_pad ("", $n - $i);
for ($ii=-$i; $ii<=$i; $ii++){
if ($i % 2 != 0 && $ii % 2 == 0)
print "&#" . rand(10025,10059) . ";";
else print $i - abs($ii) + 1;
}
print PHP_EOL;
}
✭
1
1✬1
12321
1❊3✪3✳1
123454321
1✼3✶5❃5❈3✸1
1234567654321
1✾3✯5✿7❉7✫5✷3✶1
12345678987654321
Or if you already have the string, you could do:
$n = 9; $s = "12345678987654321"; $i = 1;
while ($i <= $n)
echo str_pad ("", $n-$i) . substr ($s,0,$i - 1) . substr ($s,-$i++) . PHP_EOL;
Your code should be this:
for($i=1;$i<=3;$i++)
{
for($j=3;$j>$i;$j--)
{
echo " ";
}
for($k=1;$k<$i;$k++) /** removed = sign*/
{
echo $k;
}
if($i>=1) /**added = sign*/
{
for($m=$i; $m>=1; $m--)
{
echo $m;
}
}
echo "<br>";
}
Try this.
Details:
Your loop is not proper as in case of for($k=1;$k<=$i;$k++), this will print the
repeated number when check the condition for less then and again for equals to.
So remove the equals sign.
reason to add the eqaul sign in if($i>=1) is that the first element will not print if there will not be equals as first it will be print by for loop from where removed the equal sign.
Your output will be this:
1
121
12321
For all the x-mas lovers:
$max = 9; # can be 2 .. 9
for($i = 1; $i <= $max; $i++) {
$line = (str_pad('', $max - $i));
for($ii = 1; $ii <= $i; $ii++) {
$line .= $ii;
}
for($ii = $i-1; $ii > 0; $ii--) {
$line .= $ii;
}
echo $line . PHP_EOL;
}
Output:
1
121
12321
1234321
123454321
12345654321
1234567654321
123456787654321
12345678987654321
Amazing what computers are able to achieve nowadays! Isn't it?
A little late to the party, but here's yet another solution that uses a "for" loop with two initialization variables and a ternary-based incrementer/decrementer. It's an unorthodox use of a "for" loop, but it's still perfectly valid and arguably makes the code more elegant and easier to follow. I chose to add space before and after each semicolon and omit all other space inside the parentheses so it's easier to visualize each of the three pieces of the "for" loop (initialization, condition, increment/decrement):
$count = 9;
echo "<pre>";
for ($i=1; $i<=$count; $i++) {
echo str_pad("",$count-$i," ",STR_PAD_LEFT);
for ( $j=1,$up=true ; $j>0 ; $up?$j++:$j-- ) {
echo $j;
if ($j==$i) {$up = false;}
}
echo "<br>";
}
echo "</pre>";
Output:
1
121
12321
1234321
123454321
12345654321
1234567654321
123456787654321
12345678987654321
1
1 2 1
1 2 3 2 1
1 2 1
1
$newline = "\r\n";
$prnt = '*';
$nos = 3;
for($i = 1; $i <= 2; $i++)
{
for($s = $nos; $s >= 1; $s--)
{
echo " ";echo " ";echo " ";
}
for($j = 1; $j <= $i; $j++)
{
echo $j;echo " ";
}
$m = 2;
for($k = 1; $k <= ($i - 1); $k++)
{
echo $k;echo " ";
}
echo '<br />';
$nos--;
}
$nos = 1;
for($i = 3; $i >= 1; $i--)
{
for ($s = $nos; $s >= 1; $s--) {
echo " ";echo " ";echo " ";
}
for($j = 1; $j <= $i; $j++)
{
echo $j;echo " ";
}
for($k = 1; $k <= ($i - 1); $k++)
{
echo $k;echo " ";
}
$nos++;
echo '<br />';//printf("\n");
}
i got output
1
1 2 1
1 2 3 1 2
1 2 1
1
am not able to print space when i use echo ' '; so i used echo " "; but i dont want to use echo " "; plz resolve this issue.
I am having problem creating above program but somewhere missing a value may be need apply some condition over there . Please see my code.
Printing multiple spaces in straight HTML isn't allowed because of how it must be rendered.
If you want to print exactly - whitespaces included - just wrap your code with a <pre/> tag.
<pre>Spacing will be literal.</pre>
You could also format with the white-space CSS property by setting it to pre.
.spaces {
white-space: pre;
}
<div class="spaces">Spacing will be literal.</div>
This is a fun little algorithm. Here's a recursive solution in PHP. I wrap it in <PRE> tag so that I can use spaces and new lines "\n".
<pre>
<?php
function printPyramid($height) {
// initialize
$size = ($height * 2) - 1;
$half = $size / 2;
$arr = Array();
for($r = 0; $r < $size; $r++) {
$arr[] = array();
for($c = 0; $c < $size; $c++) {
$arr[$r][] = "";
}
}
$arr[$half][$half] = $height;
// recursively build, pass array as reference "&"
pyramidRec($arr, $half, $half, $size);
// print
for($r = 0; $r < $size; $r++) {
for($c = 0; $c < $size; $c++) {
if(empty($arr[$r][$c]))
echo " ";
else if(strlen($arr[$r][$c]) == 1)
echo "{$arr[$r][$c]} ";
else
echo $arr[$r][$c];
}
echo "\n";
}
}
function pyramidRec(&$arr, $r, $c, $size) {
$val = $arr[$r][$c];
$newVal = $val - 1;
if($newVal == 0)
return;
// up
if($r - 1 >= 0 && empty($arr[$r-1][$c])) {
$arr[$r-1][$c] = $newVal;
pyramidRec($arr, $r-1, $c, $size);
}
// down
if($r + 1 < $size && empty($arr[$r+1][$c])) {
$arr[$r+1][$c] = $newVal;
pyramidRec($arr, $r+1, $c, $size);
}
// left
if($c - 1 >= 0 && empty($arr[$r][$c-1])) {
$arr[$r][$c-1] = $newVal;
pyramidRec($arr, $r, $c-1, $size);
}
// right
if($c + 1 < $size && empty($arr[$r][$c+1])) {
$arr[$r][$c+1] = $newVal;
pyramidRec($arr, $r, $c+1, $size);
}
}
printPyramid(5);
?>
</pre>