Find the output of PHP programs | Loops | Set 4: Enhance the knowledge of PHP Loops concepts by solving and finding the output of some PHP programs.
Question 1:
<?php
$NUM = 5;
$R1 = 3;
$R2 = 1;
do
{
$R2 = $R2 * $NUM;
$R1--;
}
while ($R1 > 0);
echo $R2;
?>
Question 2:
<?php
for ($I = 1;$I <= 4;$I++)
{
for ($J = 1;$J <= $I;$J++)
{
printf("*");
}
echo '<br>';
}
?>
Question 3:
<?php
for ($I = 1;$I <= 4;$I++)
{
for ($J = 1;$J <= $I;$J++)
{
printf('I');
}
echo '<br>';
}
?>
Question 4:
<?php
for ($I = 1;$I <= 4;$I++)
{
for ($J = 1;$J <= $I;$J++)
{
print ($J);
}
for ($K = $I - 1;$K > 0;$K--)
{
print ($K);
}
echo '<br>';
}
?>
Question 5:
<?php
for ($I = 4;$I >= 1;$I--)
{
for ($K = $I;$K > 0;$K--)
{
print ($K);
}
echo '<br>';
}
?>
Answer1:-
Output:
Explanation:
The above program will print 125 on the webpage. In the above program, we created variables $NUM, $R1, and $R2 initialized with 5, 3, and 1.
Let's evaluate loop iterations:
$NUM = 5, $R1=3, $R2=1 $R2 = $R2*$NUM; $R2 = 1*4; $R2 = 5; $NUM = 5, $R1=2, $R2=5 and condition will true. $R2 = $R2*$NUM; $R2 = 5*5; $R2 = 25; $NUM = 5, $R1=1, $R2=25 and condition will true. $R2 = $R2*$NUM; $R2 = 25*5; $R2 = 125; $NUM = 5, $R1=0, $R2=25 and condition will false and loop will terminate.
Answer 2:
Output:
Explanation:
In the above program, we used nested loops. Here, we wrote a program to print the pattern of stars. The outer loop is used for the total number of rows, and the inner loop will print '*' based on the value of $I.
For $I=1 it will print one star '*'. For $I=2 it will print two stars '*'. For $I=3 it will print three stars '*'. For $I=4 it will print four stars '*'.
After every inner loop newline is printed using '<br>' tag.
Answer 3:
Output:
Explanation:
In the above program, we used nested loops. Here, we used 'I' instead of '$I' then printf() function will print 'I' instead of the value of $I.
Here, the pattern of 'I' will print on the webpage.
For $I=1 it will print one 'I'. For $I=2 it will print two 'I'. For $I=3 it will print three 'I'. For $I=4 it will print four 'I'.
After every inner loop newline is printed using '<br>' tag.
Answer 4:
Output:
Explanation:
In the above program, we used one outer-loop with a counter variable $I and two inner loops with counter variables $J and $K.
Iteration1: $I=1, The First Inner loop will print 1. The second Inner loop will not print anything. Iteration2: $I=2, The First Inner loop will print 12. The second Inner loop will print 1. Iteration3: $I=3, The First Inner loop will print 123. The second Inner loop will print 21. Iteration4: $I=4, The First Inner loop will print 1234. The second Inner loop will print 321.
After every Iteration newline is printed using '<br>' tag.
Answer 5:
Output:
Explanation:
In the above program, we used nested loops. Here, we wrote a program to print the pattern of stars. Here the outer loop is used for the total number of rows, and the inner loop will print the value of $K based on the value of $i.
For $I=4 it will print 4321. For $I=3 it will print 321. For $I=2 it will print 21. For $I=1 it will print 1.
After every inner loop newline is printed using '<br>' tag.
need an explanation for this answer? contact us directly to get an explanation for this answer