Start learning free

LESSON 53 OF 289 · CALCULATIONS - CHALLENGES

Employee of the Year V1 in C#: Integer Math and Printing

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:

        string name = "John Doe";
        int productivity = 83;
        int 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: int bonus = salary * 15 / 100;

+ turns the int into text: Console.WriteLine("Bonus: $" + bonus);

        string name = "John Doe";
        int productivity = 83;
        int salary = 135000;

        int bonus = salary * 15 / 100;
        Console.WriteLine(name);
        Console.WriteLine("Bonus: $" + bonus);

STEP 1 OF 3
    Ctrl + Enter to check

    C# · CALCULATIONS - CHALLENGES

    Employee Of The Year V1 in C#, explained

    Problem overview

    This first version of the project works with a single employee. You start with three variables: a string called name, and two ints, productivity and salary. Your program computes an end-of-year bonus equal to 15% of the salary, rounded down to a whole number, and prints two lines: the name, then Bonus: $ followed by the amount.

    For John Doe with a salary of 135000, the bonus is 15% of 135000, which is exactly 20250. The expected output is:

    John Doe
    Bonus: $20250

    The variable productivity is not used in this version. It is there because later versions of the project compare employees by it.

    Approach: multiply first, then divide

    A percentage is "out of 100", so 15% of a number is that number times 15 divided by 100. All three values here are whole numbers, and C# has two kinds of division. When both operands are int, / is integer division: it divides and throws the fraction away. The result is an int. That is exactly the "rounded down" the task asks for, so no extra rounding function is needed.

    The order matters. salary * 15 / 100 multiplies first, so the division sees the full product and loses at most one fractional part. With a salary of 135050, it gives 20257, because 135050 * 15 is 2025750 and dividing by 100 drops the .5. Writing salary / 100 * 15 divides first and throws away the last two digits of the salary before multiplying, so the same salary gives 20250. The expression runs left to right for operators of equal rank, which is why the order you write is the order it happens.

    Step by step

    1. Create bonus as an int. Declare it below the starter data and assign salary * 15 / 100. Using int as the type means the result must be a whole number, and integer division produces one.
    2. Print the name. Console.WriteLine(name) writes the text and then moves to the next line, so the name ends up alone on the first line.
    3. Print the bonus line. Join the text "Bonus: $" and the number with +. When one side of + is a string, C# converts the other side to text automatically. The dollar sign is an ordinary character inside the quotes; it only has a special meaning when a string starts with $" (an interpolated string), which this solution doesn't use.

    The solution in C#

            string name = "John Doe";
            int productivity = 83;
            int salary = 135000;
    
            int bonus = salary * 15 / 100;
            Console.WriteLine(name);
            Console.WriteLine("Bonus: $" + bonus);
    

    Details worth knowing:

    • Do not use 0.15 with an int. int bonus = salary * 0.15; does not compile: salary * 0.15 is a double, and the compiler reports Cannot implicitly convert type 'double' to 'int'. An explicit conversion exists (are you missing a cast?). A cast would work, but the integer formula is simpler and has no floating-point rounding to worry about.
    • The unused variable. Because productivity is never read, the compiler prints a warning that it is assigned but its value is never used. A warning does not stop the program from running, and the variable is meant to stay for the next versions.
    • Overflow. An int holds values up to 2,147,483,647 and wraps around silently when a result is larger. The multiplication happens before the division, so a salary of 200000000 gives a product of 3,000,000,000, which doesn't fit. With int variables the result printed is -12949672. The same calculation with long gives 30000000. Salaries in this lesson are far from that limit, but the intermediate product is where it would show up first.

    Edge cases and common mistakes

    • Rounding. Integer division truncates toward zero. A salary of 99 gives a bonus of 14 (14.85 cut down), a salary of 7 gives 1, and a salary of 0 gives 0.
    • Dividing first. salary / 100 * 15 gives 0 for a salary of 99, where the correct answer is 14.
    • Forgetting the space and colon. The expected line is exactly Bonus: $20250, with a space after the colon and no space between $ and the number.
    • Printing both on one line. Use two separate Console.WriteLine calls. Console.Write would not end the line.

    Testing

    • A salary of 135000 gives Bonus: $20250.
    • A salary of 135050 gives Bonus: $20257.
    • A salary of 99 gives Bonus: $14.
    • A salary of 0 gives Bonus: $0.
    Keep going with C#1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free