Recap - Simple Calculations
Part of the Fundamentals section of Coddy's PHP journey — lesson 21 of 71.
Challenge
EasyRead three numbers from input: basePrice, taxPercent, and quantity.
Calculate the final total by performing the following steps:
- Start with the base price
- Add the tax by multiplying the base price by
(taxPercent / 100)and adding it to the price - Multiply the result by the quantity
- Subtract a flat discount of
15from the total - Finally, add a shipping fee calculated as
quantity ** 2
Print the final total.
Example:
If the inputs are 50, 10, and 2, the output should be 99 because:
- Base price: 50
- Add 10% tax: 50 + (50 * 10 / 100) = 50 + 5 = 55
- Multiply by quantity 2: 55 * 2 = 110
- Subtract discount 15: 110 - 15 = 95
- Add shipping (2 ** 2 = 4): 95 + 4 = 99
Try it yourself
<?php
// Read input
$basePrice = intval(fgets(STDIN));
$taxPercent = intval(fgets(STDIN));
$quantity = intval(fgets(STDIN));
// TODO: Write your code below
// 1. Start with the base price
// 2. Add the tax (basePrice * taxPercent / 100)
// 3. Multiply by quantity
// 4. Subtract discount of 15
// 5. Add shipping fee (quantity ** 2)
// Output the final total
echo $finalTotal;
?>All lessons in Fundamentals
4Comparison & Logical Operators
Comparison OperatorsEquality & IdentityLogical Operators Part 1Logical Operators Part 2Recap - Simple Logic2Variables and Data Types
NumbersStrings and QuotesBooleansNaming ConventionsRecap - Variable InitEmpty VariablesString ConcatenationGetting User InputCast to Different Types5Conditional Logic
If StatementIf - ElseThe Ternary OperatorNull Coalescing OperatorSwitch StatementRecap - Making Decisions3Basic Operators
Arithmetic OperatorsModulo OperatorExponentiation OperatorCombined AssignmentIncrement/DecrementOperator PrecedenceRecap - Simple CalculationsString Operators