What the exercise asks
The editor gives you two int variables, width and height, and you print the rectangle's area. Area is width times height, so the work is one multiplication and one print. With the starting values, 23 and 17, the area is 391.
The exercise practises two things you will use constantly: combining variables with an arithmetic operator, and passing the result straight to Console.WriteLine.
Arithmetic operators on variables
C# uses the usual symbols for arithmetic: + for addition, - for subtraction, * for multiplication, / for division and % for the remainder. They work on variables exactly as they work on literal numbers:
int width = 4;
int height = 5;
Console.WriteLine(width * height); // 20
Console.WriteLine(width + height); // 9
The multiplication sign is *, not x and not a dot. Writing width height or widthheight does not multiply anything: the second form is read as one new, unknown name.
Step by step
- Use the variables that exist. Both
widthandheightare already declared and initialised, so don't declare them again. Redeclaring a name in the same scope is a compile error. - Build the expression.
width * heightis a complete expression with a value, in this case anint. - Print it.
Console.WriteLineaccepts any value, so you can put the expression between its parentheses. There is no need to store the area in a new variable first.
The solution in C#
int width = 23;
int height = 17;
Console.WriteLine(width * height);
This prints 391. The lesson's code is a list of top-level statements, so there is no Main method or class around it. The editor runs it as written, and Console is available without any extra setup.
Storing the result first
You can also keep the area in a variable. This is useful when you need it again later:
int area = width * height;
Console.WriteLine(area); // 391
Both versions print the same thing. For a single use, printing the expression directly is shorter.
What type does the result have?
When both operands are int, the product is an int too. That is fine here, but an int has a fixed range, about plus or minus 2.1 billion. If a product goes past it, C# does not stop the program. By default the value wraps around silently to a wrong, often negative, number. Two sides of 100000 would not give 10 billion. For everyday sizes you will never notice this, but it is worth knowing when the numbers get large; a long variable holds much bigger values.
If the sides can have fractions, such as a width of 2.5, declare the variables as double. The same expression, width * height, then produces a double.
Printing a label with the value
A bare number can be hard to read in a program's output. A string with $ in front lets you put expressions inside braces:
Console.WriteLine($"Area: {width * height}"); // Area: 391
This is only for readability. The exercise's checker expects the plain number, so print just width * height when you submit.
Common mistakes
- Using the wrong operator.
width + heightis half the perimeter, not the area. Check that you typed*. - Printing text instead of the value.
Console.WriteLine("width * height")prints those characters literally. Anything inside quotes is text, so leave the quotes out. - Misspelling a variable. C# is case-sensitive, so
Widthis a different name fromwidthand gives an error. - Forgetting the semicolon. Every statement ends with
;.
Trying other values
Change the starting numbers and run the code again to see how the result moves. A width of 5 and a height of 5 prints 25, and a height of 0 prints 0, since a rectangle with no height has no area. The checker runs your code with its own values, which is one more reason to use the variables rather than typing 23 * 17.
Key takeaways
- Variables can be used in arithmetic expressions just like numbers.
*multiplies; the area of a rectangle iswidth * height.- You can pass an expression directly to
Console.WriteLine; it is evaluated first and the result is printed.