Activity 3 Operators
1. Arithmetic Operations
A. Area of a rectangle
Solution:
$length = 4;
$width = 6;
$area = $length * $width;
echo "Area of a rectangle: " . $area;
Output:
Area of a rectangle: 24
B. Currency converter
Solution:
$amount = 20;
$conversion_rate = 56.21;
$converted_amount = $amount * $conversion_rate;
echo "Converted currency = " . $converted_amount;
Output:
Equivalent in USD: 1124.2
2. Assignment Operators
A. Track the progress of a fundraising campaign
Solution:
$total_donation = 0;
$new_donation = 200;
$total_donation += $new_donation;
echo "Track fundraising: " . $total_donation;
Output:
Track fundraising: 200
B. Voting system
Solution:
$candidate1_votes = 5;
$user_vote = 1;
candidate1_votes += $user_vote;
echo "Candidate 1 total votes: " . $candidate_votes;
Output:
Candidate 1 total votes: 6
3. Comparison Operators
A. Compare two athletes performance
Solution:
$athlete1 = 11.34;
$athlete2 = 10.87;
$winner = ($athlete1 < $athlete2) ? "Athlete 1": "Athlete 2";
echo "Winner: ". $winner;
Output:
Winner: Athlete 2
B. Algorithm efficiency
Solution:
$algo1 = 2.5;
$algo2 = 1.6;
$faster = ($algo1 < $algo2) ? "Algorithm 1" : "Algorithm 2";
echo "More efficient algorithm: " . $faster;
Output:
More efficient algorithm: Algorithm 2
4. Increment/Decrement Operations
A. Factorial number
Solution:
$num = 4;
$result = $num;
for ($i = $num - 1; $i > 0; $i--) {
$result *= $i;
}
echo "Factorial of $num: $result";
Output:Factorial of 4: 24
B. Vehicle movement
Solution:
$positionNow = 0;
$distanceTravelled = 23;
$positionNow += $distanceTravelled;
echo "Final position: " . $positionNow;
Output:
Final position: 23
5. Logical Operators
A. Discount elgibility
Solution:
$purchaseAmount = 800;
$customerLoyalty = true;
$discountEligibility = ($purchaseAmount > 500 && $customerLoyalty) ? "Eligible" : "Not Eligible";
echo "Discount eligibility: " . $discountEligibility;
Output
Discount eligibility: Eligible
B. Chatbot
Solution:
$userInput = "year";
$chatbotAns = ($userInput == "year") ? "It is 2024" : "I dont know that";
echo "Chatbot: " . $chatbotAns;
Output:
Chatbot: It is 2024