Account Balance After Rounded Purchase
You initially have 100 dollars in your account. You want to make a purchase, and the purchase amount will be rounded to the nearest multiple of 10. If there’s more than one nearest multiple of 10, the larger one is chosen. You need to return the account balance after making the purchase.
Approach:
- Determine the rounded amount by finding the nearest multiple of 10 to the given
purchaseAmount
. - If the
purchaseAmount
is exactly midway between two multiples of 10, choose the larger one. - Subtract the rounded amount from the initial balance of 100 dollars.
- Return the resulting balance.
Calculating the Rounded Amount:
- If
purchaseAmount
modulo 10 is less than 5, subtract it from thepurchaseAmount
to round down. - Otherwise, add the difference between 10 and
purchaseAmount
modulo 10 to round up.
Now, let’s put this into code:
|
|
Key Takeaways:
- The problem involves a basic understanding of rounding to the nearest multiple.
- The solution uses the modulo operation to find the remainder and then performs rounding accordingly.
- The code is clear and concise, addressing the requirements of the problem within the given constraints.