For Loop Exercises, Practice, Solution

1

Create a script to construct the following pattern, using nested for loop.

*  
* *  
* * *  
* * * *  
* * * * *

Click Here To See Solution
2

Create a script to construct the following pattern, using a nested for loop.

* 
* * 
* * * 
* * * * 
* * * * * 
* * * * * 
* * * * 
* * * 
* * 
* 
Click Here To See Solution
3

Write a PHP script that creates the following table using for loops. Add cellpadding="3px" and cellspacing="0px" to the table tag.

1 * 1 = 11 * 2 = 21 * 3 = 31 * 4 = 41 * 5 = 5
2 * 1 = 22 * 2 = 42 * 3 = 62 * 4 = 82 * 5 = 10
3 * 1 = 33 * 2 = 63 * 3 = 93 * 4 = 123 * 5 = 15
4 * 1 = 44 * 2 = 84 * 3 = 124 * 4 = 164 * 5 = 20
5 * 1 = 55 * 2 = 105 * 3 = 155 * 4 = 205 * 5 = 25
6 * 1 = 66 * 2 = 126 * 3 = 186 * 4 = 246 * 5 = 30
Click Here To See Solution
4
(1) Create a script that displays 1-2-3-4-5-6-7-8-9-10 on one line. There will be no hyphen(-) at starting and ending position.
Sample Solution:
Click Here To See Solution
5

Create a script that displays 1-2-3-4-5-6-7-8-9-10 on one line. There will be no hyphen(-) at starting and ending position.

1-2-3-4-5-6-7-8-9-10

Click Here To See Solution
6
(2) Create a script to construct the following pattern, using nested for loop.

*  
* *  
* * *  
* * * *  
* * * * * 
Click Here To See Solution
7

Create a script using a for loop to add all the integers between 0 and 30 and display the sum.

The sum of the numbers 0 to 30 is 465

Click Here To See Solution
8
(3) Write a PHP program to generate and display the first n lines of a Floyd triangle. (use n=5 and n=11 rows).
According to Wikipedia Floyd's triangle is a right-angled triangular array of natural numbers, used in computer science education. It is named after Robert Floyd. It is defined by filling the rows of the triangle with consecutive numbers, starting with a 1 in the top left corner:
Sample output for n = 5 :
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Click Here To See Solution
9
(4)  Create a script to construct the following pattern, using a nested for loop.

* * 
* * * 
* * * * 
* * * * * 
* * * * * 
* * * * 
* * * 
* * 
*
Click Here To See Solution
10 (5) Write a program to calculate and print the factorial of a number using a for loop. The factorial of a number is the product of all integers up to and including that number, so the factorial of 4 is 4*3*2*1= 24.
Click Here To See Solution
11
(6) Write a PHP script that creates the following table using for loops. Add cellpadding="3px" and cellspacing="0px" to the table tag.

1 * 1 = 1 1 * 2 = 2 1 * 3 = 3 1 * 4 = 4 1 * 5 = 5
2 * 1 = 2 2 * 2 = 4 2 * 3 = 6 2 * 4 = 8 2 * 5 = 10
3 * 1 = 3 3 * 2 = 6 3 * 3 = 9 3 * 4 = 12 3 * 5 = 15
4 * 1 = 4 4 * 2 = 8 4 * 3 = 12 4 * 4 = 16 4 * 5 = 20
5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25
6 * 1 = 6 6 * 2 = 12 6 * 3 = 18 6 * 4 = 24 6 * 5 = 30
Click Here To See Solution
12
(7) Write a PHP program to print alphabet pattern 'A'.
Expected Output:

  ***                                                       
 *   *                                                      
 *   *                                                      
 *****                                                      
 *   *                                                      
 *   *                                                      
 *   *                                                      
 *   * 
Click Here To See Solution
13
(8) Write a PHP program to print alphabet pattern 'X'.
Expected Output:

 *   *                                                      
 *   *                                                      
  * *                                                       
   *                                                        
  * *                                                       
 *   *                                                      
 *   *
Click Here To See Solution
14
(9) Write a PHP program to print alphabet pattern 'R'.
Expected Output:

 ****                                                       
 *   *                                                      
 *   *                                                      
 ****                                                       
 * *                                                        
 *  *                                                       
 *   *
Click Here To See Solution
15
(10) Write a PHP script using nested for loop that creates a chess board as shown below.
Use table width="270px" and take 30px as cell height and width.

chess board(OutPut)

Click Here To See Solution
16
(11)Find all odd numbers between 1 to 100 using loop.
 <?php
Click Here To See Solution
17
(12) Find the Sum of even and odd numbers between 1 to 100.
<?php
Click Here To See Solution
So come must
Delivered is to ye belonging enjoyment preferred. Astonished and acceptance men two discretion...