Skip to content
Search lessons, topics, tests…
Esc

    ↑ ↓ moveEnter openEsc close
    Lesson3 / 20

    Calculations: choose the numbers and keep the units

    You studied for 150 minutes. How would the program show 2 hours and 30 minutes? Start with the smaller calculations below; keep the units beside your numbers.

    Predict four results

    An expression produces a value. A value can be a literal such as 7, a variable such as totalHours, or the result of a calculation. Here sessions means the number of study sessions, so totalHours / sessions asks for hours per session.

    C#
    using System;
    
    int sessions = 2;
    int totalHours = 7;
    
    Console.WriteLine(totalHours / sessions);
    Console.WriteLine(7m / sessions);
    Console.WriteLine(3 + 4 * 2);
    Console.WriteLine((3 + 4) * 2);

    What does the first WriteLine print?

    A 3.5
    B 3
    C 4
    D 7
    Show answer

    Correct answer: B

    Both operands are int values. Integer division gives the integer quotient; 7 divided by 2 produces 3 here.

    Option A: A fractional result needs a suitable fractional numeric type during the division.

    Option C: This is not rounding to the nearest whole number.

    Option D: The expression divides 7 by 2.

    Hint: Look at the operand types before looking at the destination.

    The type affects the calculation

    The suffix m makes 7m a decimal literal. The second division therefore uses decimal arithmetic and produces 3.5. Use decimal when the values and arithmetic need decimal fractions, such as many money calculations. Choosing decimal does not remove the need to think about rounding rules for a real application.

    For the positive durations in this lesson, integer division tells us how many complete groups fit. More generally, C# integer division truncates the quotient toward zero. Decide whether you need whole groups or a fractional answer before performing the division. Storing the integer result in a decimal variable afterwards cannot restore the lost fraction.

    ExpressionOperation happens firstResult
    7 / 2int division3
    7m / 2decimal division3.5
    3 + 4 * 2multiply, then add11
    (3 + 4) * 2parentheses, then multiply14

    Multiplication and division have priority over addition and subtraction. Parentheses make the intended grouping explicit. Use them when they help a reader see your intention, especially when translating a written requirement into code. The examples use a dot as the decimal separator; regional display settings can show a comma while representing the same result.

    What is the result of (3 + 4) * 2?

    A 11
    B 10
    C 14
    D 9
    Show answer

    Correct answer: C

    The parentheses produce 7 first, then multiplication produces 14.

    Option A: 11 is the result of 3 + 4 * 2 without those parentheses.

    Option B: Adding 3 and 4 produces 7, not 5.

    Option D: The expression multiplies the sum by 2.

    Draw a duration as groups

    Text
    150 minutes = [60] [60] [30]
                    2 whole hours  +  30 minutes left

    The division operator / counts the complete 60-minute groups. The remainder operator % gives the part left after those groups. Keep the units beside the calculation: minutes divided by minutes-per-hour gives hours; the remainder is still minutes.

    Build the conversion yourself

    Write a program that stores 150 total minutes, calculates whole hours and remaining minutes, then prints one labelled sentence. Use variables for the two results. Compare your attempt with this complete solution:

    C#
    using System;
    
    int totalMinutes = 150;
    int wholeHours = totalMinutes / 60;
    int remainingMinutes = totalMinutes % 60;
    
    Console.WriteLine($"{wholeHours} hours and {remainingMinutes} minutes");

    For 150 total minutes, which pair is correct?

    A wholeHours = 150, remainingMinutes = 60
    B wholeHours = 2, remainingMinutes = 30
    C wholeHours = 30, remainingMinutes = 2
    D wholeHours = 2, remainingMinutes = 0
    Show answer

    Correct answer: B

    150 contains two full groups of 60, leaving 30 minutes.

    Option A: Those are the original duration and the group size, not the computed results.

    Option C: The whole-hour count and remainder have been swapped.

    Option D: Two groups use 120 minutes, so 30 remain.

    Now change only totalMinutes to 135. Predict first: the sentence should say 2 hours and 15 minutes. Try 60 and 0 as well; they should produce 1 hour with 0 remaining minutes and 0 hours with 0 remaining minutes. Our output template always prints the word hours; language polishing can come after the calculation is correct.

    Explain back: When is integer division useful? Why would changing the type of a variable after dividing fail to recreate a fraction?

    References: Microsoft — arithmetic operators, numeric conversions.

    Quick reference

    • Match the numeric type to the calculation you need.
    • int / int performs integer division; decimal division can retain a fractional result.
    • The m suffix marks a decimal literal.
    • Parentheses make grouping explicit.
    • For nonnegative minutes: total / 60 gives whole hours; total % 60 gives leftover minutes.
    • Include units in names and output.
    Sign in to mark lessons done and keep your place in the course.Sign in

    TechInterviewNotes Editorial Team · Updated 27 Oct 2026 ·