Start learning free

LESSON 188 OF 289 · PROJECTS: EVERYDAY PROGRAMS

Shopping Cart V1 in C#: A One-Item Receipt

Practice Read the full explanation

Practice variables, arithmetic, and string concatenation by printing a tiny one-item receipt.

You start with:

        string item = "Coffee Mug";
        int price = 12;
        int quantity = 3;

Build:

Coffee Mug x3
Total: $36

Rules:

  • Compute total = price * quantity.
  • First line: "<item> x<quantity>" (e.g. "Coffee Mug x3").
  • Second line: "Total: $<total>".
  • Join the two lines with a newline "\n" into one string result, then print it with Console.WriteLine(result).

C#'s + on a string and an int converts the integer to text for you.

line1 + "\n" + line2 joins both lines into one string.

        string item = "Coffee Mug";
        int price = 12;
        int quantity = 3;

        int total = price * quantity;
        string line1 = item + " x" + quantity;
        string line2 = "Total: $" + total;
        string result = line1 + "\n" + line2;
        Console.WriteLine(result);

STEP 1 OF 5
    Ctrl + Enter to check

    C# · PROJECTS: EVERYDAY PROGRAMS

    Shopping Cart V1 in C#, explained

    Problem overview

    This project practises variables, arithmetic and string concatenation by printing a tiny receipt for one item. You start with item (a string), price and quantity (both int). The program computes total as price * quantity, builds two lines, joins them with a newline into one string called result and prints it with Console.WriteLine(result).

    The first line reads <item> x<quantity> and the second reads Total: $<total>. For three Coffee Mugs at 12 each:

    Coffee Mug x3
    Total: $36

    Approach: numbers, then lines, then one string

    The work splits into three layers. First the arithmetic: the total is the price of one mug counted once per mug, price * quantity. Then the text: each row of the receipt is its own string, assembled from pieces with +. Finally the joining: the two rows go into result with a newline between them, and one print statement writes it all.

    Giving every row its own named variable (line1, line2) keeps each statement short and checkable on its own. When the receipt grows, new rows are added the same way.

    How + builds the text

    When at least one operand of + is a string, C# joins text. If the other operand is a number, it is converted to text first. So item + " x" + quantity produces Coffee Mug x3: the name, then a string that starts with a space and an x, then the quantity as text. The space before the x is part of the second piece, " x". The total line works the same way: "Total: $" + total. The dollar sign is an ordinary character inside the quotes.

    Step by step

    1. Create total as an int: price * quantity.
    2. Create line1, a string: the item, then " x", then the quantity.
    3. Create line2, a string: "Total: $" followed by total.
    4. Create result: line1, the newline "\n" and line2. The escape sequence \n is one newline character, which starts a new row wherever it sits in the text.
    5. Print result with Console.WriteLine. Each row appears on its own line.

    The solution in C#

            string item = "Coffee Mug";
            int price = 12;
            int quantity = 3;
    
            int total = price * quantity;
            string line1 = item + " x" + quantity;
            string line2 = "Total: $" + total;
            string result = line1 + "\n" + line2;
            Console.WriteLine(result);
    

    Details worth knowing:

    • Order of evaluation. * binds tighter than +, so "Total: $" + price * quantity also gives Total: $36. But "Total: $" + price + quantity goes left to right and joins text twice, giving Total: $123. Computing total in its own statement first avoids the trap.
    • Two kinds of +. Between two ints, + adds. As soon as one side is a string, it joins. 1 + 2 + "x" gives 3x, while "x" + 1 + 2 gives x12.
    • Strings are immutable. Each + creates a new string. result is a new string built from copies of line1 and line2, and neither of them changes.
    • Integer arithmetic. int is a 32-bit type. If the product doesn't fit, the value wraps around silently: with price 2000000000 and quantity 3, the product prints as 1705032704. Real prices here are far smaller.
    • Console.WriteLine adds a final newline. The text in result contains one \n in the middle, and WriteLine supplies the one at the end.

    Edge cases and common mistakes

    • Zero quantity. A Pen priced 2 with quantity 0 prints Pen x0 and Total: $0.
    • Forgetting the space in " x". You get Coffee Mugx3.
    • Quoting a variable name. "quantity" is the word quantity, not its value.
    • Printing the lines separately. Two Console.WriteLine calls print the same text here, but the task asks for one joined string, result.
    • Missing newline. Without "\n" the receipt runs together on one line: Coffee Mug x3Total: $36.
    • Extra spaces. The total line is Total: $36, with a space after the colon and none after the dollar sign.

    Testing

    • "Coffee Mug", 12, 3 gives Coffee Mug x3 and Total: $36.
    • "Notebook", 4, 10 gives Notebook x10 and Total: $40.
    • "Pen", 2, 0 gives Pen x0 and Total: $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