Start learning free

LESSON 161 OF 413 · PROJECTS: EVERYDAY PROGRAMS

Shopping Cart V1 - Single Item Total in C#

Practice

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

    Shopping Cart V1 in C#

    Keep going with C#About 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free