Activity 3

Problem 1 - Arithmetic Operations:

A. Write a PHP script to calculate the area of a rectangle using arithmetic operators. Display the result.

Solution:
// Create variables to hold the width and length of the rectangle
$width = 2;
$length = 3;

//Create a variable to hold the value of the operation
$area = $width * $length;

//Echo the result
echo "Area: $area";

Output:

Area: 6


B. Implement a script that converts currency from one denomination to another using arithmetic operators. Display the converted amount.


Solution:
//Declare variables for amount and rate of conversion
$amount = 20;
$conversion_rate = 0.50;

//Declare a variable in which the operation takes place
$converted_amount = $amount * $conversion_rate;
//Echo or print the results
echo "Converted amount: " . $converted_amount;

Output:

Converted amount: 10


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.

Solution:

//Declare a variable for the base fund and the new donations
$total_donation = 200000;
$new_donation = 2024;

//Add the new donations to the base fund using asignment operator
$total_donation += $new_donation;

//Print the result
echo "Track fundraising: ₱" . $total_donation;

Output:

Track fundraising: ₱202024


B. Implement a voting system where candidate votes are incremented based on user input. Display the final vote count for each candidate.

Solution:

//Declare a variable for both the candidate and user votes
$candidate_votes = 20;
$user_votes = 2024;

//Add the user votes to the candidate votes using asignment operator
$candidate_votes += $user_votes;

//Print the result
echo "Total votes: " . $candidate_votes;

Output:

Total votes: 2044


Problem 3 - Comparison Operators:

A. Compare the performance of two athletes in a race and determine the winner using comparison operators. Display the result.

Solution:

//Declare variables for the two athletes
$athleteOne = 4.69;
$athleteTwo = 4.95;

//Compare who got the least time and print who won

echo "The winner is ";
if ($athleteOne < $athleteTwo)
{
        echo "Athlete One!";
}
else
{
        echo "Athlete Two";
}

Output:

The winner is Athlete One!


B. Evaluate the efficiency of two algorithms based on their execution times using comparison operators. Display the more efficient algorithm.

Solution:

//Declare variables for the two algorithms
$algo1 = 4.69;
$algo2 = 4.95;

//Compare who got the least time executing a program

echo "The faster execution time is the ";
if ($algo1 < $algo2)
{
        echo "first algorithm";
}
else
{
        echo "second algorithm";
}

Output:

The faster execution time is the first algorithm


Problem 4 - Increment/Decrement Operations:

A. Calculate the factorial of a number using postfix increment/decrement operations. Display the factorial.

Solution:

//Declare variables
$num = 5;
$factorial = 1;

//Create a loop to determine the factorial of the given number

for ($i = 1; $i <= $num; $i++)
       {
              $factorial = $factorial * $i;
       }

//Print the result
echo "The factorial of $num is $factorial";

Output:

The factorial of 5 is 120


B. Simulate the movement of a vehicle along a track using prefix increment/decrement operations. Display the final position of the vehicle.

Solution:

//Declare variables
$vehicle_placement = 0;
$meters_to_move = 20;

//Create the loop to simulate the car's movement
while ($meters_to_move != 0)
       {
              $vehicle_placement += 1;
              --$meters_to_move;
       }

//Print result
echo "The vehicle has moved " . $vehicle_placement . " meters forward";

Output:

The vehicle has moved 20 meters forward


Problem 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.

Solution:

//Create variables
$purchase_amount = 2000;
$customer_loyalty = 8;
$discount = $customer_loyalty * 0.1;

//If-else method to determine eligibility
if ($purchase_amount >= 1000 && $customer_loyalty >= 5)
{
$purchase_amount -= $discount;
echo "Customer eligible for discount";
}
else
{
echo "Customer is not eligible for discount";
}

Output:

Customer eligible for discount

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.

Solution:

//Create variables
$user_query = "weather";

//Create conditions
$response = ($user_query == "weather") ? "Today's weather is sunny." : "I don't understand your query.";

//Echo result
echo "Chatbot response: $response\n";

Output:

Chatbot response: Today's weather is sunny.