How to get following output using PHP - php

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>';
}

Related

Wap a program to print 3 rows and 3 columns from 1 to 12 in php

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>";
}

PHP loop problems,how do i solv this

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;
}
}

How to loop number (input:5) in php

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>";
}
?>

Print pyramid pattern

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>

How to alter loop behavior for index 3, 5 and 15?

I don't want to display the numbers 3, 5 and 15. I want you to show when my program runs
1 2 three 4 five 6 7 ...
$var1 = 3;
$var2 = 5;
$var3 = 15;
for ($i=1; $i<=100; $i++) {
if($i<=3){
if (($i%$var1)==0)
echo 'Three' .'<br/>';
}
if($i<=5)
if (($i%$var2)==0) {
echo 'Five' .'<br/>';
}
if($i<=15)
if ((($i%$var3)==0)) {
echo 'ThreeFive' .'<br/>';
}
echo $i.'<br/>';
}
A rather crude, but working solution would be this:
$var1 = 3;
$var2 = 5;
$var3 = 15;
for( $i=0; $i<100; $i++ ) {
switch( $i ) {
case $var1: echo 'Three<br>'; break;
case $var2: echo 'Five<br>'; break;
case $var3: echo 'ThreeFive<br>'; break;
default: echo $i.'<br>';
}
}
check with this code
$exclude_array = array(3,5,15);
for($i = 0;$i < 100; $i++){
if(!in_array($i,$exclude_array))
echo $i;
}
A little bit more elegant solution...
$alter = array(3=>"Three<br>", 5=>"Five<br>", 15=>"ThreeFive<br>");
for($i=0; $i<100; $i++) echo isset($alter[$i]) ? $alter[$i] : $i;
I suppose you wanted to print Three and Five every time the $i is divisible by 3 or 5. Then you may alter it this way:
$alter = array(3=>"Three", 5=>"Five");
for($i=0; $i<100; $i++) {
$print_word = false;
foreach($alter as $key=>$val) if($i>0 && $i%$key==0) {
echo $val;
$print_word = true;
}
echo $print_word ? "<br>" : $i;
}
Try this-
$var1 = 3;
$var2 = 5;
$var3 = 15;
for ($i=1; $i<=100; $i++) {
if ($i==$var1)
echo 'Three' .'<br/>';
else if ($i==$var2) {
echo 'Five' .'<br/>';
}
else if ($i==$var3) {
echo 'ThreeFive' .'<br/>';
}
else
echo $i.'<br/>';
}
Try this one :)
<?php
$var1 = 3;
$var2 = 5;
$var3 = 15;
for ($i=1; $i<=100; $i++) {
if($i == $var1){
echo "three <br />";
}elseif($i == $var2){
echo "five <br />";
}elseif($i == $var3){
echo "Fifteen <br />";
}else{
echo $i."<br />";
}
}
?>

Categories