Start learning free

LESSON 200 OF 289 · PROJECTS: EVERYDAY PROGRAMS

Report Card V1 in C#: Averaging with Integer Division

Practice Read the full explanation

Compute a student's average score across three tests using integer division so the result is a whole number.

You start with:

int test1 = 85;
int test2 = 92;
int test3 = 78;

Build:

Average: 85

Rules:

  • Compute the integer average of test1, test2, and test3 using integer division (int divided by int already drops the remainder).
  • Print "Average: " followed by the average, for example Console.WriteLine("Average: " + average);

C#'s int / int truncates, so (test1 + test2 + test3) / 3 already gives an integer.

int test1 = 85;
int test2 = 92;
int test3 = 78;

int average = (test1 + test2 + test3) / 3;
Console.WriteLine("Average: " + average);

STEP 1 OF 2
    Ctrl + Enter to check

    C# · PROJECTS: EVERYDAY PROGRAMS

    Report Card V1 in C#, explained

    Problem overview

    This is the first version of the report card project. A student has taken three tests, and their scores are stored in three int variables: test1, test2 and test3. The program computes the average score as a whole number and prints it in the form Average: 85.

    With the starting scores 85, 92 and 78, the three scores add up to 255, and 255 divided by 3 is exactly 85. So the program prints:

    Average: 85

    The lesson is about one idea: when both sides of a / are int, C# does integer division and discards the fractional part. That is what lets the average come out as a whole number without any rounding code.

    Approach: add first, then divide

    An average is the sum of the values divided by how many there are. Here there are three values, so the plan has two parts: add the three scores, then divide the total by 3.

    The order matters. In C#, / binds tighter than +, exactly as in school arithmetic. Written without parentheses, test1 + test2 + test3 / 3 divides only test3 by 3 and then adds the rest. With the starting data that expression gives 85 + 92 + 26 = 203, which is not an average at all. The parentheses around the sum force the addition to happen first.

    Because every operand is an int, the result of the division is an int too. C# throws the remainder away: it truncates toward zero. For scores that are never negative, this means the average is rounded down. Scores of 70, 71 and 71 add up to 212, and 212 divided by 3 is 70.67 in exact arithmetic, but the program stores 70.

    Step by step

    1. Declare average as an int. Its value is (test1 + test2 + test3) / 3. The three starter variables stay untouched above it, and the new line goes below them. Storing the result in an int variable is natural here, because the division already produces an int.
    2. Print the result. Call Console.WriteLine with "Average: " + average. The text "Average: " ends with a space, so the number follows it directly. When + joins a string and an int, C# converts the number to its text form first, and no explicit conversion is needed.

    The program reads as a short story: the scores, the calculation, the printed line. Keeping the calculation in its own named variable, rather than putting the whole expression inside the print call, keeps the calculation separate from the printing, so each step can be checked on its own.

    The solution in C#

            int test1 = 85;
            int test2 = 92;
            int test3 = 78;
    
            int average = (test1 + test2 + test3) / 3;
            Console.WriteLine("Average: " + average);
    

    Details worth knowing:

    • The type of a division depends on its operands. int / int is an integer division. If either side is a double, the division is done in floating point: (70 + 71 + 71) / 3.0 gives 70.66666666666667. To keep that precision you would store the result in a double, but this lesson asks for the whole number.
    • Assigning to a double does not rescue an integer division. In double d = (7 + 2) / 2; the division happens first, on two int values, and gives 4. Only then is 4 converted to 4.0. The precision is already lost.
    • Truncation, not rounding. Dividing 100 + 100 + 99 = 299 by 3 gives 99, even though the exact value 99.67 is closer to 100.
    • int is 32 bits. Sums of test scores are tiny compared to its range, so overflow is not a concern here.

    Edge cases and common mistakes

    • Missing parentheses. test1 + test2 + test3 / 3 compiles and runs, but it computes the wrong number. The output is Average: 203 for the starter data.
    • Dividing by the wrong number. The divisor is the count of tests, 3. Using 2 or 100 gives a valid-looking number that is wrong.
    • Expecting a decimal. If you see 85 where you expected 85.0 or 70.67, the cause is the integer division described above, and it is the intended behaviour in this lesson.
    • Forgetting the space after the colon. The output has to read Average: 85, so the string literal is "Average: " with its trailing space.

    Testing

    Change the three starter values and check the output against a hand calculation of the sum divided by 3, dropping the remainder:

    • 85, 92, 78 print Average: 85.
    • 70, 71, 71 print Average: 70.
    • 100, 100, 99 print Average: 99.
    • 0, 0, 2 print Average: 0.

    The next version builds on this one.

    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