[HackerRank] Day 2: Operators

Problem ๐Ÿ”—

Sample Input

12.00
20
8

Sample Output

15

Explanation

Given:

meal_cost = 12, tip_percent = 20, tax_percent = 8

Calculations:

tip = 12 * (20 / 100) = 2.4

tax = 12 * (8 / 100) = 0.96

total_cost = meal_cost + tip + tax = 12 + 2.4 + 0.96 = 15.36

round(total_cost) = 15

We round total_cost to the nearest integer and print the result, 15.

Solution

void solve(double meal_cost, int tip_percent, int tax_percent) {
    double tip = meal_cost * tip_percent / 100;
    double tax = meal_cost * tax_percent / 100;
    double total_cost = meal_cost + tip + tax;
    
    cout << (int)round(total_cost) << endl;
}

round(total_cost)๋กœ round ์กฐ๊ฑด์„ ํ•ด๊ฒฐํ•˜๋ฉด์„œ (int)๋ฅผ ์‚ฌ์šฉํ•ด์„œ double์„ int๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค.

Leave a comment