Area and Perimeter of Rectangle

Beginner-friendly C++ program that calculates the area and perimeter of a rectangle using length and width provided by the user.

BeginnerTopic: Basic Programs
Back

C++ Area and Perimeter of Rectangle Program

This program helps you to learn the fundamental structure and syntax of C++ programming.

Try This Code
#include <iostream>
                  using namespace std;
                  
                  int main() {
                      float length, width, area, perimeter;
                      
                      cout << "Enter length of rectangle: ";
                      cin >> length;
                      
                      cout << "Enter width of rectangle: ";
                      cin >> width;
                      
                      area = length * width;
                      perimeter = 2 * (length + width);
                      
                      cout << "Area of rectangle = " << area << " square units" << endl;
                      cout << "Perimeter of rectangle = " << perimeter << " units" << endl;
                      
                      return 0;
                  }
Output
Enter length of rectangle: 5
                  Enter width of rectangle: 3
                  Area of rectangle = 15 square units
                  Perimeter of rectangle = 16 units

Understanding Area and Perimeter of Rectangle

This program calculates the area and perimeter of a rectangle. These are fundamental geometric calculations used in mathematics, engineering, architecture, and many real-world applications. The program demonstrates how to apply mathematical formulas in programming, handle user input, and perform basic arithmetic operations.

---

1. What is a Rectangle?

A rectangle is a four-sided shape (quadrilateral) with:

Four right angles (90 degrees each)
Opposite sides equal and parallel
Two pairs of equal sides: length and width

Properties:

-

Length (l):

The longer side (or one of the equal longer sides)

-

Width (w):

The shorter side (or one of the equal shorter sides)

All angles are 90 degrees
Opposite sides are parallel

Examples in real life:

Doors, windows, books, screens, tables

---

2. Understanding Area and Perimeter

Area:

The amount of space inside the rectangle
Measured in square units (cm², m², inches², etc.)
Formula:

Area = length × width

Represents the "coverage" or "space occupied"

Perimeter:

The total distance around the boundary of the rectangle
Measured in linear units (cm, m, inches, etc.)
Formula:

Perimeter = 2 × (length + width)

Represents the "fence length" around the rectangle

Visual example:

Rectangle with length = 5, width = 3
Area = 5 × 3 =

15 square units

(space inside)

Perimeter = 2 × (5 + 3) =

16 units

(distance around)

---

3. Header File: #include <iostream>

#include <iostream>

This header provides:

cout → for displaying output
cin → for reading input from the user

Essential for any program that needs user interaction.

---

4. Declaring Variables

float length, width, area, perimeter;

Why use `float` instead of `int`?

Variable `length` and `width`:

Can be decimal values (e.g., 5.5, 3.25, 10.75)
Real-world measurements often have decimals
float handles both integers and decimals

Variable `area` and `perimeter`:

Store calculated results
May be decimal even if inputs are integers
Example: length = 5, width = 3 → area = 15.0 (still float)

Examples of decimal inputs:

Length = 5.5 meters, Width = 3.25 meters
Area = 5.5 × 3.25 = 17.875 square meters
int would truncate to 17 (wrong!)

---

5. Taking Input From User

`cout << "Enter length of rectangle: ";`

cin >> length;

`cout << "Enter width of rectangle: ";`

cin >> width;

Step 1: Get length

Prompts user to enter length
Reads and stores in length

Step 2: Get width

Prompts user to enter width
Reads and stores in width

Example:

User enters: length =

5

, width =

3

length = 5.0, width = 3.0

---

6. Calculating Area

area = length * width;

Mathematical formula:

Area = length × width

How it works:

Multiplies length by width
Result is the area in square units

Step-by-step (length = 5, width = 3):

area = 5.0 * 3.0
area = 15.0

Why "square units"?

Area is two-dimensional (length × width)
Units are squared: cm × cm = cm²
Represents "coverage" or "space"

Real-world example:

Room: 5m × 3m = 15 m² (15 square meters)
Carpet needed: 15 square meters

---

7. Calculating Perimeter

perimeter = 2 * (length + width);

Mathematical formula:

Perimeter = 2 × (length + width)

Why this formula?

Rectangle has 4 sides
Two sides of length "length"
Two sides of length "width"
Total = length + width + length + width
Simplified: 2 × (length + width)

Step-by-step (length = 5, width = 3):

perimeter = 2 * (5.0 + 3.0)
perimeter = 2 * 8.0
perimeter = 16.0

Visual breakdown:

Rectangle: length = 5, width = 3

Perimeter = 5 + 3 + 5 + 3 = 16

Or: 2 × (5 + 3) = 16 ✅

---

8. Displaying the Results

cout << "Area of rectangle = " << area << " square units" << endl;

cout << "Perimeter of rectangle = " << perimeter << " units" << endl;

Output format:

Area: "Area of rectangle = 15 square units"
Perimeter: "Perimeter of rectangle = 16 units"

Why "square units" and "units"?

We don't know the actual unit (cm, m, inches, etc.)
"Square units" indicates area (two-dimensional)
"Units" indicates perimeter (one-dimensional)
Makes program universal for any measurement system

---

9. Complete Example Walkthrough

Input:

length = 5, width = 3

Step 1: Read inputs

length = 5.0
width = 3.0

Step 2: Calculate area

area = 5.0 * 3.0 = 15.0

Step 3: Calculate perimeter

perimeter = 2 * (5.0 + 3.0) = 2 * 8.0 = 16.0

Step 4: Display results

-

"Area of rectangle = 15 square units"

-

"Perimeter of rectangle = 16 units"

Verification:

Area: 5 × 3 = 15 ✅
Perimeter: 2 × (5 + 3) = 16 ✅

---

10. More Examples

Example 1: Square (special rectangle)

Length = 4, Width = 4
Area = 4 × 4 =

16 square units

Perimeter = 2 × (4 + 4) =

16 units

Example 2: Decimal measurements

Length = 5.5, Width = 3.25
Area = 5.5 × 3.25 =

17.875 square units

Perimeter = 2 × (5.5 + 3.25) =

17.5 units

Example 3: Large rectangle

Length = 100, Width = 50
Area = 100 × 50 =

5,000 square units

Perimeter = 2 × (100 + 50) =

300 units

---

11. Real-World Applications

Construction:

Calculating floor area for carpet/tiles
Finding perimeter for fencing

Architecture:

Room dimensions
Building layouts

Landscaping:

Garden bed area
Fence length calculation

Interior Design:

Wall area for paint
Floor space planning

---

12. Relationship Between Area and Perimeter

Important note:

Same area can have different perimeters
Example: Area = 16
4 × 4: Perimeter = 16 (square)
8 × 2: Perimeter = 20 (rectangle)
16 × 1: Perimeter = 34 (long rectangle)

Square has minimum perimeter for given area!

---

Summary

Area = length × width (space inside rectangle)
Perimeter = 2 × (length + width) (distance around)
Use float to handle decimal measurements
Area measured in square units, perimeter in linear units
These formulas are fundamental in geometry and real-world applications

This program teaches:

Applying mathematical formulas in code
Using float for decimal numbers
Basic arithmetic operations
Real-world problem solving
Geometry concepts

Understanding area and perimeter helps in:

Construction and architecture
Interior design
Landscaping
Many practical applications
Advanced geometry problems

This is a perfect beginner program that demonstrates how mathematical concepts translate directly into programming, and it's used in countless real-world scenarios.

Let us now understand every line and the components of the above program.

Note: To write and run C++ programs, you need to set up the local environment on your computer. Refer to the complete article Setting up C++ Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your C++ programs.

Practical Learning Notes for Area and Perimeter of Rectangle

This C++ program is part of the "Basic Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.

A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.

For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.

Table of Contents