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
- Create
bonusas anint. Declare it below the starter data and assignsalary * 15 / 100. Usingintas the type means the result must be a whole number, and integer division produces one. - 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. - Print the bonus line. Join the text
"Bonus: $"and the number with+. When one side of+is astring, 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.15with anint.int bonus = salary * 0.15;does not compile:salary * 0.15is adouble, and the compiler reportsCannot 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
productivityis 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
intholds 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. Withintvariables the result printed is -12949672. The same calculation withlonggives 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 * 15gives 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.WriteLinecalls.Console.Writewould 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.
