Question: Create a PHP file and include necessary statements to complete the following 4 problems: 1. Define two variables and assign two positive numbers as their

Create a PHP file and include necessary statements to complete the following 4 problems:

1. Define two variables and assign two positive numbers as their values.

Compute their sum, difference, and product.

Use echo statement to display the sum, difference, and product of those values.

Sample output:

Number 1: 10

Number 2: 20

Sum: 30

Difference: -10

Product: 200

2. Define an array of 10 numbers. Use a loop to display square of each element in the array.

Sample output:

Number: 2, Square: 4

Number: 5, Square: 25

. . . 3. Define an array of 10 numbers between -50 and 50. Include some duplicate values.

  1. Use print_r( ) function to print the array.
  2. Use sort( ) and rsort( ) functions to print values in the ascending and descending order.
  3. Use array_unique( ) function to print unique values. Note: array_unique( ) method returns an array of unique values.

Sample output:

Original array:

Array ( [0] => 30 [1] => 3 [2] => -25 [3] => -5 [4] => 20 [5] => 40 [6] => -15 [7] => -10 [8] => 20 [9] => 30 )

Sorted array in the ascending order:

Array ( [0] => -25 [1] => -15 [2] => -10 [3] => -5 [4] => 3 [5] => 20 [6] => 20 [7] => 30 [8] => 30 [9] => 40 )

Sorted array in the descending order:

Array ( [0] => 40 [1] => 30 [2] => 30 [3] => 20 [4] => 20 [5] => 3 [6] => -5 [7] => -10 [8] => -15 [9] => -25 )

Unique values:

Array ( [0] => 40 [1] => 30 [3] => 20 [5] => 3 [6] => -5 [7] => -10 [8] => -15 [9] => -25 )

4. Display the sum of the positive numbers in the array defined in part 3.

Sample output:

Sum of the positive values: 143

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!