Problem 1: Arithmetic Operations

A. Write a PHP script to calculate the area of a rectangle using arithmetic operators. Display the result. (area of a rectangle: $area = $length * $width; )

B. Implement a script that converts currency from one denomination to another using arithmetic operators. Display the converted amount. (Convert currency: $converted_amount = $amount * $conversion_rate;)

Solution

A.
$length = 5;
$width = 10;
$area = $length * $width;
echo "The area of the triangle is: " . "$area"
B.
$amount_usd = 100;
$conversion_rate_hkd = 7.82; // per HKD
$converted_amount = $amount_usd * $conversion_rate_hkd;
echo "USD converted to HKD: " . "$converted_amount"

Output

A.
The area of the triangle is: 50
B.
USD converted to HKD: 782

Problem 2: Assignment Operators:

A. Track the progress of a fundraising campaign by updating the donation total dynamically using combined assignment operators. Display the updated total. (Track fundraising: $total_donation += $new_donation;)

B. Implement a voting system where candidate votes are incremented based on user input. Display the final vote count for each candidate. (Voting system: $candidate_votes += $user_vote;)

Solution

A.
$total_donation = 110;
$new_donation = 259; $total_donation += $new_donation;
echo "total donation is: " . "$total_donation"
B.
$candidate1_votes = 419;
$candidate2_votes = 468;
$user1_vote = 1; // voted for 1
$user2_vote = 2; // voted for 2
$candidate1_votes += $user1_vote;
$candidate2_votes += $user2_vote;
echo "candidate 1 final votes: $candidate1_votes" . "candidate 2 final votes: $candidate2_votes";

Output

A.
total donation is: 369
B.
candidate 1 final votes: 420
candidate 2 final votes: 470

Problem 3: Comparison Operators:

A. Compare the performance of two athletes in a race and determine the winner using comparison operators. Display the result. (Compare athlete times: Use '<' to compare times.)

B. Evaluate the efficiency of two algorithms based on their execution times using comparison operators. Display the more efficient algorithm. (Evaluate algorithm efficiency: Use '<' to compare times.)

Solution

A.
$athlete_1 = 50.4; // sec
$athlete_2 = 46.2; // sec
$winner = ($athlete_1 < $athlete_2) ? "Athlete 2 " : "Athlete 1";
echo "The winner is: $winner";
B.
$algo1_time = 1;
$algo2_time = 4;
$faster_algo = ($algo1_time < $algo2_time) ? "Algorithm 2 " : "Algorithm 1";
echo "The faster algorithm is: $faster_algo";

Output

A.
The winner is: Athlete 1
B.
The faster algorithm is: Algorithm 2

4. Increment/Decrement Operations:

A. Calculate the factorial of a number using postfix increment/decrement operations. Display the factorial. (Calculate factorial: Use a loop to multiply numbers.)

B. Simulate the movement of a vehicle along a track using prefix increment/decrement operations. Display the final position of the vehicle. (Simulate vehicle movement: Use += for forward movement.)

Solution

A.
$num = 4;
$factorial = 1;
for ($i = 1; $i <= $num; $i++)
{
$factorial *= $i;
}
echo "factorial of $num is: $factorial";
+ B.
$spot = 0;
$distance = 5; //kilometers
$spot += $distance;
echo"The final position of the vehicle is: $spot ";

Output

A.
factorial of 4 is: 24B.
The final position of the vehicle is: 5 km

5. Logical Operators:

A. Determine eligibility for a discount based on purchase amount and customer loyalty using logical operators. Display whether the customer is eligible for a discount. (Determine discount eligibility: Use && to check conditions.)

B. Design a decision-making system for a chatbot to respond to user queries using logical operators. Display the appropriate response based on the query. (Chatbot decision-making: Use ? : for simple conditions.)

Solution

A.
$purchase_amount = 100;
$customer_loyalty = true;

$discount_eligible = $purchase_amount > 50 && $customer_loyalty;

if ($discount_eligible) {

echo "Customer is eligible for a discount.";
} else { echo "Customer is not eligible for a discount.";
}

B.
$user_query = "What is the weather like today?";

$response = strpos($user_query, "weather") !== false ? "It's sunny." : "I'm sorry, I don't understand your query.";

echo $response;


Output

A.
Customer is eligible for a discount.
B.
It's sunny.

© 2024 Ceejay Nilles. All rights reserved.