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.
C++ Area and Perimeter of Rectangle Program
This program helps you to learn the fundamental structure and syntax of C++ programming.
#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;
}Enter length of rectangle: 5
Enter width of rectangle: 3
Area of rectangle = 15 square units
Perimeter of rectangle = 16 unitsUnderstanding 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:
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)
Examples in real life:
---
2. Understanding Area and Perimeter
Area:
Area = length × width
Perimeter:
Perimeter = 2 × (length + width)
Visual example:
15 square units
(space inside)
16 units
(distance around)
---
3. Header File: #include <iostream>
#include <iostream>
This header provides:
cout → for displaying outputcin → for reading input from the userEssential 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`:
float handles both integers and decimalsVariable `area` and `perimeter`:
Examples of decimal inputs:
int would truncate to 17 (wrong!)---
5. Taking Input From User
cin >> length;
cin >> width;
Step 1: Get length
lengthStep 2: Get width
widthExample:
5
, width =
3
length = 5.0, width = 3.0---
6. Calculating Area
area = length * width;
Mathematical formula:
Area = length × width
How it works:
Step-by-step (length = 5, width = 3):
area = 5.0 * 3.0area = 15.0 ✅Why "square units"?
Real-world example:
---
7. Calculating Perimeter
perimeter = 2 * (length + width);
Mathematical formula:
Perimeter = 2 × (length + width)
Why this formula?
Step-by-step (length = 5, width = 3):
perimeter = 2 * (5.0 + 3.0)perimeter = 2 * 8.0perimeter = 16.0 ✅Visual breakdown:
Perimeter = 5 + 3 + 5 + 3 = 16
---
8. Displaying the Results
cout << "Area of rectangle = " << area << " square units" << endl;
cout << "Perimeter of rectangle = " << perimeter << " units" << endl;
Output format:
Why "square units" and "units"?
---
9. Complete Example Walkthrough
Input:
length = 5, width = 3
Step 1: Read inputs
length = 5.0width = 3.0Step 2: Calculate area
area = 5.0 * 3.0 = 15.0Step 3: Calculate perimeter
perimeter = 2 * (5.0 + 3.0) = 2 * 8.0 = 16.0Step 4: Display results
-
"Area of rectangle = 15 square units"
-
"Perimeter of rectangle = 16 units"
Verification:
---
10. More Examples
Example 1: Square (special rectangle)
16 square units
16 units
Example 2: Decimal measurements
17.875 square units
17.5 units
Example 3: Large rectangle
5,000 square units
300 units
---
11. Real-World Applications
Construction:
Architecture:
Landscaping:
Interior Design:
---
12. Relationship Between Area and Perimeter
Important note:
Square has minimum perimeter for given area!
---
Summary
float to handle decimal measurementsThis program teaches:
float for decimal numbersUnderstanding area and perimeter helps in:
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.