Calculate the Area of a Rectangle in CSharp

Calculate the Area of a Rectangle in C#


Introduction

In programming, one of the most practical applications is solving mathematical problems. A rectangle is a common shape in geometry, and its area is calculated by multiplying its length by its width. In this lesson, we will create a simple C# program that calculates the area of a rectangle. This will help you understand how variables, data types, and basic arithmetic operations work in a real-world context.


Objectives

Before diving into the code, let’s focus on the goals of this lesson. By the end of this activity, you should be able to:

  1. Understand – Explain how variables and arithmetic operators are used to calculate the area of a rectangle in C#.
  2. Learn – Identify and apply the correct syntax for declaring variables and performing multiplication in C#.
  3. Practice – Write and run a program in VS Code that computes and displays the area of a rectangle.
  4. Apply – Modify the program to handle user input and calculate the area of different rectangles.

Beginner-Friendly Source Code & Step-by-Step Instructions

Step 1: Open VS Code and Terminal

  • Make sure the .NET SDK is installed (dotnet --version).
  • Create/open your class folder (e.g., D:\2025-1_doe_bsis_1a).

Step 2: Create a new console project

dotnet new console -n rectangle_area
cd rectangle_area

Step 3: Replace Program.cs with the code below

using System;

namespace RectangleAreaApp
{
    class Program
    {
        static void Main(string[] args)
        {
            // Declare variables for length and width
            int length = 5;
            int width = 10;

            // Calculate the area
            int area = length * width;

            // Display the result
            Console.WriteLine("Rectangle Area Calculation:");
            Console.WriteLine("Length: " + length);
            Console.WriteLine("Width: " + width);
            Console.WriteLine("Area: " + area);
        }
    }
}

Step 4: Run the program

dotnet run

Example Output

Rectangle Area Calculation:
Length: 5
Width: 10
Area: 50

Summary

In this lesson, you learned how to use variables, data types, and arithmetic operations in C#. By calculating the area of a rectangle, you practiced storing values, performing multiplication, and displaying the output. This foundational skill will help you build more complex applications that require mathematical problem-solving.


Multiple Choice Quiz

  1. Which formula is used to calculate the area of a rectangle?
    A. length + width
    B. length * width
    C. length / width
    D. length – width
  2. In C#, which data type is best suited for whole numbers like length and width?
    A. string
    B. int
    C. double
    D. char
  3. If length = 6 and width = 4, what is the area?
    A. 10
    B. 24
    C. 12
    D. 20
  4. Which C# symbol is used for multiplication?
    A. *
    B. x
    C. mul
    D. #
  5. Why do we use variables in this program?
    A. To store values that can change
    B. To create loops
    C. To handle errors
    D. To stop execution

Exercises, Assessment & Lab Exam

Intro

To further develop your programming skills, here are exercises designed to help you modify and improve the rectangle area program. These tasks will also prepare you for solving real-world problems involving user input and dynamic values.

Exercises

  1. Modify the program to use user input for length and width (Console.ReadLine).
  2. Change the program to calculate the area using double data type for decimals.
  3. Display the perimeter of the rectangle in addition to the area.
  4. Ask the user for two sets of values and display both areas.
  5. Format the output to show: “The area of a rectangle with length X and width Y is Z.”

Assessment

  • Quiz: Answer the 5 multiple-choice questions above.
  • Performance Task: Create a program that calculates both the area and perimeter of a rectangle using user input.

Lab Exam

  • Write a C# program that asks the user to input the length and width of a rectangle.
  • The program should calculate and display:
    • Area = length * width
    • Perimeter = 2 * (length + width)

You may visit our Facebook page for more information, inquiries, and comments. Please subscribe also to our YouTube Channel to receive free capstone projects resources and computer programming tutorials.

Hire our team to do the project.

, , , , , , , , , ,

Post navigation