Start learning free

LESSON 201 OF 289 · PROJECTS: EVERYDAY PROGRAMS

Report Card V2 in C#: Weighted Score with Integer Math

Practice Read the full explanation

Combine variables and arithmetic to compute a student's weighted final score.

Weights: Homework 20%, Midterm 30%, Final 50%. Each weighted part is rounded down to a whole number before the parts are added.

You start with:

int homework = 90;
int midterm = 82;
int finalExam = 88;

Build:

Score: 86

Rules:

  • Compute score = homework * 20 / 100 + midterm * 30 / 100 + finalExam * 50 / 100.
  • Each part is rounded down because int divided by int drops the remainder (the third variable is named finalExam).
  • For the starting data: 18 + 24 + 44 = 86.
  • Print "Score: " followed by the score, for example Console.WriteLine("Score: " + score);

The third variable is named finalExam. int / int truncates, so each weight already rounds down.

int homework = 90;
int midterm = 82;
int finalExam = 88;

int score = homework * 20 / 100 + midterm * 30 / 100 + finalExam * 50 / 100;
Console.WriteLine("Score: " + score);

STEP 1 OF 2
    Ctrl + Enter to check

    C# · PROJECTS: EVERYDAY PROGRAMS

    Report Card V2 in C#, explained

    Problem overview

    The first version averaged three tests with equal importance. In this version the three marks count differently. Homework is worth 20% of the final score, the midterm 30% and the final exam 50%. The program holds the marks in three int variables, homework, midterm and finalExam, and prints the weighted score as a whole number.

    The rule is that each weighted part is rounded down before the parts are added. With the starting marks 90, 82 and 88, the parts are:

    • Homework: 90 times 20 divided by 100 is 18.
    • Midterm: 82 times 30 divided by 100 is 24, because 24.6 is cut down to 24.
    • Final exam: 88 times 50 divided by 100 is 44.

    The sum is 18 + 24 + 44 = 86, so the program prints:

    Score: 86

    Approach: mark times weight, divided by 100

    A weight written as a percentage is a fraction of 100. Taking 30% of a mark means multiplying by 30 and dividing by 100. Working in whole numbers like this avoids decimals completely: every step is an int operation, and int / int in C# drops the remainder.

    The order inside each part matters. Multiply first, then divide: midterm * 30 / 100 is evaluated from left to right, so the multiplication happens before the division. With a mark of 82 that is 2460 / 100 = 24. If you divided first, 30 / 100 would already be an integer division with the result 0, and every part would become 0. The expression midterm * (30 / 100) is exactly that mistake.

    The lesson also fixes the rounding rule: each part is rounded down on its own, then the three results are added. That differs from adding the exact values and rounding once at the end. The exact weighted score for 90, 82 and 88 is 86.6, and the rule gives 86, but the two ways can disagree. For three marks of 99, the parts are 19, 29 and 49, which add up to 97. Adding the exact parts (19.8 + 29.7 + 49.5) gives 99. The program has to follow the rounding rule of the statement, part by part.

    Step by step

    1. Declare score as an int. Place it below the three starter variables.
    2. Write the homework part as homework * 20 / 100. This is 20% of the homework mark, rounded down.
    3. Add the midterm part, midterm * 30 / 100, and the final exam part, finalExam * 50 / 100. The third variable is named finalExam, so use that name in the expression.
    4. Check the weights. 20, 30 and 50 add up to 100, so the three parts together make up the whole score. A perfect set of 100s gives 20 + 30 + 50 = 100.
    5. Print the result with Console.WriteLine as "Score: " + score. The string ends with a space after the colon.

    The solution in C#

            int homework = 90;
            int midterm = 82;
            int finalExam = 88;
    
            int score = homework * 20 / 100 + midterm * 30 / 100 + finalExam * 50 / 100;
            Console.WriteLine("Score: " + score);
    

    Details worth knowing:

    • Operator precedence. * and / have the same precedence and are evaluated left to right, and both bind tighter than +. So the single line is read as three separate parts added together, with no parentheses needed.
    • Each division is separate. The expression has three divisions, and each one truncates its own result. That is exactly the rounding rule of the lesson, and it is why 82 gives 24 and not 24.6.
    • Avoid floating point here. homework * 0.2 + midterm * 0.3 + finalExam * 0.5 gives 86.6 for the starter data. The result would be a double, and turning it back into an int would round the total once, not each part.
    • Overflow is not a risk. A mark of 100 times 50 is only 5000, far below the int limit of about 2.1 billion.

    Edge cases and common mistakes

    • Dividing the weight first. homework * (20 / 100) is homework * 0, so it evaluates to 0 for any mark. Keep the multiplication before the division.
    • Rounding the total once. Using exact arithmetic and rounding at the end gives different answers for some marks. The lesson rounds each part.
    • Weights that do not add up. A typo such as 20, 30 and 40 makes the score of a perfect student fall short of 100.
    • Wrong variable name. The third variable is finalExam. A name that was never declared, such as final, is a compile error.

    Testing

    Try these marks, listed as homework, midterm and final exam:

    • 90, 82, 88 print Score: 86.
    • 99, 99, 99 print Score: 97, as every part is cut down.
    • 33, 33, 33 print Score: 31, from the parts 6, 9 and 16.
    • 10, 10, 10 print Score: 10, because every part is exact (2, 3 and 5).
    • 1, 1, 1 print Score: 0, because every part rounds down to 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