Calculate the Area of a Rectangle in C#
Table of Contents
- Calculate the Area of a Rectangle in C#
- Introduction
- Objectives
- Beginner-Friendly Source Code & Step-by-Step Instructions
- Step 1: Open VS Code and Terminal
- Step 2: Create a new console project
- Step 3: Replace Program.cs with the code below
- Step 4: Run the program
- Example Output
- Summary
- Multiple Choice Quiz
- Exercises, Assessment & Lab Exam
- Intro
- Exercises
- Assessment
- Lab Exam
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:
- Understand – Explain how variables and arithmetic operators are used to calculate the area of a rectangle in C#.
- Learn – Identify and apply the correct syntax for declaring variables and performing multiplication in C#.
- Practice – Write and run a program in VS Code that computes and displays the area of a rectangle.
- 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
- Which formula is used to calculate the area of a rectangle?
A. length + width
B. length * width
C. length / width
D. length – width - In C#, which data type is best suited for whole numbers like length and width?
A. string
B. int
C. double
D. char - If
length = 6
andwidth = 4
, what is the area?
A. 10
B. 24
C. 12
D. 20 - Which C# symbol is used for multiplication?
A. *
B. x
C. mul
D. # - 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
- Modify the program to use user input for length and width (
Console.ReadLine
). - Change the program to calculate the area using double data type for decimals.
- Display the perimeter of the rectangle in addition to the area.
- Ask the user for two sets of values and display both areas.
- 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.