PHP find output programs (Loops) | set 4

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.
Submitted by Nidhi, on January 21, 2021

Question 1:

<?php
    $NUM = 5;
    $R1 = 3;
    $R2 = 1;
    
    do
    {
        $R2 = $R2 * $NUM;
        $R1--;
    }
    
    while ($R1 > 0);

    echo $R2;
?>

Output:

125

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.

Question 2:

<?php
    for ($I = 1;$I <= 4;$I++)
    {
        for ($J = 1;$J <= $I;$J++)
        {
            printf("*");
        }
        echo '<br>';
    }
?>

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.

Question 3:

<?php
    for ($I = 1;$I <= 4;$I++)
    {
        for ($J = 1;$J <= $I;$J++)
        {
            printf('I');
        }
        echo '<br>';
    }
?>

Output:

I
II
III
IIII

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.

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

Output:

1
121
12321
1234321

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.

Question 5:

<?php
    for ($I = 4;$I >= 1;$I--)
    {
        for ($K = $I;$K > 0;$K--)
        {
            print ($K);
        }
        echo '<br>';
    }
?>

Output:

4321
321
21
1

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.





Comments and Discussions




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.