Start learning free

LESSON 16 OF 347 · CALCULATIONS WITH NUMBERS

Area of a Rectangle in C#: Multiplying Two Variables

Lesson Read the full explanation

We created two variables to store the width and the height of a rectangle.

On line 3, use these two variables and an arithmetic operation you just learned to print the area of the rectangle to the console.

STEP 1 OF 1
    Ctrl + Enter to check

    C# · CALCULATIONS WITH NUMBERS

    Area Of Rectangle in C#, explained

    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

    1. Use the variables that exist. Both width and height are already declared and initialised, so don't declare them again. Redeclaring a name in the same scope is a compile error.
    2. Build the expression. width * height is a complete expression with a value, in this case an int.
    3. Print it. Console.WriteLine accepts 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 + height is 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 Width is a different name from width and 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 is width * height.
    • You can pass an expression directly to Console.WriteLine; it is evaluated first and the result is printed.
    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