I want the following output using for loop in php - php

I want to display the output as like this
1 1
12 21
123 321
1234 4321
1234554321
Here is my php code
for($i=1;$i <= 5;$i++)
{
for($j=1;$j<=$i;$j++)
{
// print the result
echo "$j";
}
for($y=$i;$y<=$i;$y++)
{
echo ' ';
}
for($k=$i;$k>=1;$k--)
{
// print the result
echo " $k";}
echo "<br/>";
}
But I got the output like this
1 1
12 2 1
123 3 2 1
1234 4 3 2 1
12345 5 4 3 2 1
Please help me get an output like above.

try
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/>";
}
output:-
1 1
12 21
123 321
1234 4321
1234554321
For browser view :- for($y=0;$y<(5-$i)*4;$y++) else correct way to going is for($y=0;$y<(5-$i)*2;$y++)

Did you ever here about the PHP functions str_repeat and range?
http://php.net/manual/en/function.str-repeat.php.
http://php.net/manual/en/function.range.php
With it you can print your character like this:
echo str_repeat(' ', (5 -$i) *4);
Complete code
$count = 5; // count oft rows and oft iterated numbers
for ($i = 1; $i <= $count; $i++) {
echo implode('', range(1, $i));
echo str_repeat(' ', ($count -$i) *4);
echo implode('', array_reverse(range(1, $i)));
echo '<br />';
}

How about this?
for($i=1;$i<=5;$i++){
echo substr("12345", 0, $i);
echo str_repeat(5-$i, ' ');
echo str_reverse(substr("12345", 0, $i));
echo '<br>';
}

Try this:
for($i=1;$i <= 5;$i++)
{
for($j=1;$j<=$i;$j++)
{
echo "$j";
}
for($y=1;$y<=10-(2*$i);$y++)
{
echo ' ';
}
for($k=$i;$k>=1;$k--)
{
// print the result
echo "$k";
}
echo "<br/>";
}
OUTPUT:
1 1
12 21
123 321
1234 4321
1234554321

How about this with $y<= 2 * (5 - i),
for($i=1;$i <= 5;$i++) {
for($j=1;$j<=$i;$j++) {
echo "$j";
}
for($y=1;$y<= 2 * (5 - i); $y++) {
echo ' ';
}
for($k=$i;$k>=1;$k--) {
echo "$k";
}
echo "<br/>";
}

for ($counter=1;$counter<= 5;$conter++)
{
for ($j=1;$j<=$counter;$j++)
{
echo "$j";
}
for ($y=1;$y<=10-(2*$counter);$y++)
{
echo ' ';
}
for ($k=$counter;$k>=1;$k--)
{
echo "$k";
}
echo "<br/>";
}

Related

for loop if else Break php

I'd like to add page like this :
foreach($list->result() as $row){
if($no % 12 == 0 ) {
// Add Page Break Here
echo "<p>break</p>";
}
if($no % 6 == 0 ) {
//// $pdf->Ln();
echo "<br><br><br>";
}
echo $no;
echo "<br>";
$no++;
}
I want if this data > 12 page break , if this data < 12 not add page but if data > 6 add more space
My problem is, it's always like this :
1
2
3
4
5
space
space
space
6
7
8
9
10
11
break
space
space
space
12
This can be easily fixed by:
foreach ($list->result() as $row)
{
if ($row['SOMETHING??'] >= 12)
{
/// Add Page Break Here
echo "<p>break</p>";
}
if ($row['SOMETHING??'] >= 6 && $row['SOMETHING??'] < 12)
{
//// $pdf->Ln();
echo "<br><br><br>";
}
echo $no;
echo "<br>";
$no++;
}
Look carefully at the comparison operators which are being used.
Documentation:
http://php.net/manual/en/language.operators.comparison.php
You just need to re-arrange the order of what you are doing, you are outputting the breaks Before you output the line they relate to
foreach($list->result() as $row){
echo $no;
echo "<br>";
if ($no % 12 == 0 ) {
// Add Page Break Here
echo "<p>break</p>";
}
if ($no % 6 == 0 ) {
//// $pdf->Ln();
echo "<br><br><br>";
}
$no++;
}
You may also need to stop the %6 test doing something when the %12 line will have done something like so
foreach($list->result() as $row){
echo $no;
echo "<br>";
if ($no % 12 == 0 ) {
// Add Page Break Here
echo "<p>break</p>";
}
if ($no % 6 == 0 && $no % 12 != 0) {
//// $pdf->Ln();
echo "<br><br><br>";
}
$no++;
}

loop with symbol minus and number php

i have this code .
echo "<br>";
$start = 1;
$angka = $_POST[angka];
$a = $angka;
for($i=$start; $i<=$angka; $i++) {
for($j=$start;$j<=$angka;$j=$j+2){
echo $i;
if($j < $angka) echo $a;
}
$a--;
echo '<br>';
}
Reference: Print Looping for dynamic row php
This is not my expecting result .
First i want the result like this .
-2-4-
1-3-5
-2-4-
1-3-5
-2-4-
The rule is Number of rows and columns follow the number of numbers declared.If the declaration of the number 5, then the results will be displayed are 5 rows and 5 columns like the example above.
i think this code is working
<?php
$_POST['angka'] = 5;
$angka = $_POST['angka'];
for($i=1; $i<=$angka; $i++) {
for($j=1;$j<=$angka;$j++){
if($i%2 == 1) {
if($j%2 == 0) {
echo $j;
} else {
echo '-';
}
} else {
if($j%2 == 0) {
echo '-';
} else {
echo $j;
}
}
}
echo '<br>';
}
Result:
-2-4-
1-3-5
-2-4-
1-3-5
-2-4-

Not getting output as expected in printing star pattern in php

I have tried this code and output i am getting is here
<?php
for($i=1;$i<=5;$i++){
for($j=5-$i;$j>=1;$j--){
echo " ";
echo " ";
}
for($k=1;$k<=$i;$k++){
echo $i;
}
echo "<br/>";
}
?>
1
22
333
4444
55555
but i want output like this, please tell where i am wrong
1
12
123
1234
12345
Just change echo $i to echo $k,
for($k=1;$k<=$i;$k++){
echo $k;
}
Since you work with HTML, you may want to wrap this logic into a function that could let you choose the number of lines to display. You can tweak it to fit your needs in term of CSS :
function numberTriangle($endNumber, $align_text = 'right') {
$max = intval($endNumber);
$align = strval($align_text);
$align = ( strlen(trim($align)) > 0 ) ? $align : 'right';
echo "<div style='text-align : $align; display : inline-block'>";
if( $max > 0 ) {
for( $i = 1; $i < $max + 1; $i++ ) {
for( $j = 1; $j < $i + 1; $j++ ) {
echo $j;
}
echo '<br />';
}
}
echo '</div>';
}
numberTriangle(5);
// Will display :
/*
1
12
123
1234
12345
*/
Thanks to this function you do not have to add manual spaces anymore, which is cleaner (and saves you headhaches !).

To print 1 to 25

Expected output:
1 2 3 4 5
10 9 8 7 6
11 12 13 14 15
20 19 18 17 16
21 22 23 24 25
Tried following code:
for($i=1;$i<=25;$i++)
{
if($i%5 ==0)
{
echo $i;
echo "<br>";
for($j=($i+5);$j>$i;$j--)
{
echo $j;
}
echo "<br>";
}
else if ($i%5!=0)
{
echo $i;
}
}
for($i=1;$i<=25;$i++)
{
if($i%5 ==0)
{
echo $i;
echo "<br>";
for($j=($i+5);$j>$i;$j--)
{
echo $j;
}
$i = $i+5;
echo "<br>";
}
else if ($i%5!=0)
{
echo $i;
}
}

How to identify duplicate numbers

So i have this problem where i generate random numbers from 1 - 10 then displaying the numbers then identifying what is repeating. BTW intNcases is the number of numbers and should not exceed 20. This is actually our assignment and i'm really having a hard time with it please help. This is my code so far.
Sample Output
Random numbers of case is: 7
Random numbers are: 4, 2, 1, 1, 4,3,2
Numbers: 4, 2, 1, 3
Repeating numbers are: 4, 2, 1
<html>
<body>
<?php
$intNcases = 5;
$hold = array(0,0,0);
$temp = array(0,0,0);
$rep = array(0,0,0);
$num = array(0,0,0);
$count = 1;
if($intNcases>20)
{
echo 'Error N cases is greater than 20';
}
else
{
echo 'The number of case/s is: '. $intNcases;
echo '<br><br>'. 'Input'.'<br>';
for($x=0;$x<$intNcases;$x++)
{
$N = rand(1,10);
echo $N. '<br>';
$hold[$x] = $N;
$temp[$x] = $N;
}
echo 'OUTPUT<br>';
for($d=0;$d<$intNcases;$d++)
{
for($j=1;$j<$intNcases;$j++)
{
if($hold[$d] == $temp[$j])
{
$rep[$j-1] = $hold[$j-1];
$hold[$j-1] = 0;
}
else
{
$num[$j-1] = $hold[$j-1];
}
}
echo '#'.$count.' - '.$num[$d]. '<br>';
$count++;
}
echo 'Repeating numbers are: ';
for($k=0;$k<sizeof($rep);$k++)
{
echo $rep[$k]. ' ';
}
}
?>
</body>
</html>
Maybe you can do this more easier with array_unique or array_shuffle or other array functions
You could try this.
$intcases = rand(1,20);
$numbers = array();
echo 'Random number of cases: '. $intcases . '</br>';
echo 'Random numbers are: ';
for($x=0;$x<$intcases;$x++)
{
$number = rand(1,10);
echo $number. ' ';
if(array_key_exists($number, $numbers))
{
$numbers[$number] = $numbers[$number] + 1;
}else
{
$numbers[$number] = 1;
}
}
echo '</br>';
echo 'Numbers are: ';
foreach($numbers as $number => $x)
{
echo $number . ' ';
}
echo '</br>';
echo 'Repeating numbers: ';
foreach($numbers as $key => $value)
{
if($value > 1)
{
echo $key . ' ';
}
}
echo '</br>';

Categories