Start learning free

LESSON 54 OF 292 · CALCULATIONS - CHALLENGES

Employee of the Year V1 in Swift: Bonus from a Salary

Practice Read the full explanation

Compute an end-of-year performance bonus for one employee, then print the employee's name and the bonus.

You start with:

        let name = "John Doe"
        let productivity = 83
        let salary = 135000

Build:

John Doe
Bonus: $20250

Rules:

  • The bonus is 15% of the salary, rounded down to a whole number.
  • Print the employee's name on the first line.
  • Print the bonus on the second line as "Bonus: $<amount>".
  • productivity is not used in this version. It is there for the next versions, where you will compare employees by productivity.

Dividing two whole numbers (Int) drops the fraction, so salary * 15 / 100 is already rounded down: let bonus = salary * 15 / 100

Convert the number with String(...) (or use \(...) inside the text): print("Bonus: $\(bonus)")

        let name = "John Doe"
        let productivity = 83
        let salary = 135000

        let bonus = salary * 15 / 100
        print(name)
        print("Bonus: $\(bonus)")

STEP 1 OF 3
    Ctrl + Enter to check

    SWIFT · CALCULATIONS - CHALLENGES

    Employee Of The Year V1 in Swift, explained

    Problem overview

    This first version of the Employee of the Year project handles a single employee. The starter code gives three constants: name, productivity and salary. The program computes the employee's end-of-year bonus, which is 15% of the salary rounded down to a whole number, and prints two lines: the name, then Bonus: $ followed by the amount.

    With salary = 135000, 15% is 20250, so the program prints:

    John Doe
    Bonus: $20250
    

    The productivity constant is not used in this version. It is part of the data so the next versions can compare employees; the next version builds on this one.

    Approach: do the percentage with whole numbers

    A percentage is "multiply by the rate, divide by 100". Swift divides two Int values as integer division: the fraction is cut off, so the result is already rounded down. That is exactly the rounding the rules ask for, and no floating-point type or rounding function is needed.

    The order matters. salary * 15 / 100 multiplies first, so the division sees the full product. Writing salary / 100 * 15 would drop the fraction too early: with a salary of 199 it gives 15, while the correct rounded-down bonus is 29.

    The program then needs two prints. The name goes out as it is. The bonus is a number, so it has to become text before it can sit next to Bonus: $.

    Step by step

    1. Create bonus with let. The value is computed once and never changes, so a constant fits. It is salary * 15 / 100.
    2. Print the name on its own line. print adds the line break itself.
    3. Print the bonus line. The text Bonus: $ and the number go into one string literal, with the number inserted by interpolation, \(bonus).

    The solution in Swift

            let name = "John Doe"
            let productivity = 83
            let salary = 135000
    
            let bonus = salary * 15 / 100
            print(name)
            print("Bonus: $\(bonus)")
    

    The Swift details worth knowing:

    • Interpolation. Inside a string literal, \(expression) is replaced by the value's text. It works for any value that can be printed, so no conversion call is needed. The $ has no special meaning in Swift strings; it is an ordinary character.
    • No Int plus String. Swift won't join a number to text with +. Writing "Bonus: $" + bonus fails to compile: binary operator '+' cannot be applied to operands of type 'String' and 'Int'. Interpolation, or "Bonus: $" + String(bonus), fixes it.
    • Type inference. salary is an Int because the literal 135000 has no decimal point, and bonus is an Int because it comes from Int arithmetic. If salary were written as 135000.0, it would be a Double, and the division would keep the fraction.
    • Constants. Everything here is a let. Swift reports an error if code tries to assign to a let a second time.

    Edge cases and common mistakes

    • Small salaries round down. With salary = 99, the product is 1485 and the division gives 14, so the program prints Bonus: $14.
    • Dividing first. As above, salary / 100 * 15 loses the remainder before the multiplication. Multiply first.
    • Printing both values in one call. print(name, bonus) would put them on one line, separated by a space. The output needs two separate lines, so there are two print calls.
    • Using productivity. It plays no part in the bonus here. Leave the constant as it is.

    Testing

    • salary = 135000 prints John Doe and Bonus: $20250.
    • salary = 99 prints John Doe and Bonus: $14.
    Keep going with Swift1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free