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
- Create
totalas anint:price * quantity. - Create
line1, astring: the item, then" x", then the quantity. - Create
line2, astring:"Total: $"followed bytotal. - Create
result:line1, the newline"\n"andline2. The escape sequence\nis one newline character, which starts a new row wherever it sits in the text. - Print
resultwithConsole.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 * quantityalso givesTotal: $36. But"Total: $" + price + quantitygoes left to right and joins text twice, givingTotal: $123. Computingtotalin its own statement first avoids the trap. - Two kinds of
+. Between twoints,+adds. As soon as one side is astring, it joins.1 + 2 + "x"gives3x, while"x" + 1 + 2givesx12. - Strings are immutable. Each
+creates a new string.resultis a new string built from copies ofline1andline2, and neither of them changes. - Integer arithmetic.
intis a 32-bit type. If the product doesn't fit, the value wraps around silently: withprice2000000000 andquantity3, the product prints as 1705032704. Real prices here are far smaller. Console.WriteLineadds a final newline. The text inresultcontains one\nin the middle, andWriteLinesupplies the one at the end.
Edge cases and common mistakes
- Zero quantity. A Pen priced 2 with quantity 0 prints
Pen x0andTotal: $0. - Forgetting the space in
" x". You getCoffee Mugx3. - Quoting a variable name.
"quantity"is the word quantity, not its value. - Printing the lines separately. Two
Console.WriteLinecalls 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 x3andTotal: $36. - "Notebook", 4, 10 gives
Notebook x10andTotal: $40. - "Pen", 2, 0 gives
Pen x0andTotal: $0.
