Calculating the area of a rectangle is one of the most fundamental exercises in programming and geometry. In C++, this exercise helps beginners practice variables, input/output, arithmetic operations, and functions. Knowing how to compute the area is useful in a variety of real-world applications, such as designing graphics, calculating land dimensions, or managing layouts in software development.

with hands-on learning.
get the skills and confidence to land your next move.
Program 1: Area of a Rectangle Using User Input
This program allows the user to enter the length and width of a rectangle. It then calculates the area using the formula area = length × width, demonstrating basic arithmetic and input/output in C++.
#include <iostream>
using namespace std;
int main() {
double length, width, area;
cout << "Enter length of the rectangle: " << endl;
cin >> length;
cout << "Enter width of the rectangle: " << endl;
cin >> width;
area = length * width;
cout << "The area of the rectangle is: " << area << endl;
return 0;
}
This approach teaches beginners how to handle user input, perform calculations, and display the results. It is a simple yet effective way to understand how variables interact in a program.
Program 2: Area Calculation Using Predefined Values
Sometimes the rectangle’s dimensions are known beforehand. This program calculates the area using predefined length and width, making it useful for testing or automated calculations.
#include <iostream>
using namespace std;
int main() {
double length = 8.0; // predefined length
double width = 5.0; // predefined width
double area = length * width;
cout << "For length " << length << " and width " << width
<< ", the area of the rectangle is: " << area << endl;
return 0;
}
Here, the program uses constants for the rectangle’s dimensions. Beginners can focus on the arithmetic operation without worrying about input handling, helping them understand basic program logic.
Program 3: Using a Function to Calculate Area
Functions make code reusable and organized. This program defines a function to calculate the area of a rectangle, which can be called with any set of length and width values.
#include <iostream>
using namespace std;
double calculateArea(double length, double width) {
return length * width;
}
int main() {
double length, width;
cout << "Enter length of the rectangle: " << endl;
cin >> length;
cout << "Enter width of the rectangle: " << endl;
cin >> width;
double area = calculateArea(length, width);
cout << "The area of the rectangle is: " << area << endl;
return 0;
}
Using a function separates the calculation logic from the main program flow. Beginners learn how to create modular code, which is easier to read, maintain, and reuse for multiple rectangles.
Program 4: Calculating Areas for Multiple Rectangles Using Vectors
When working with multiple rectangles, a vector can store several lengths and widths and calculate areas for all of them efficiently.
#include <iostream>
#include <vector>
using namespace std;
double calculateArea(double length, double width) {
return length * width;
}
int main() {
vector<pair<double, double>> rectangles {{3.0, 4.0}, {5.0, 6.0}, {7.0, 8.0}};
cout << "Calculating areas for multiple rectangles:" << endl;
for(auto rect : rectangles) {
double area = calculateArea(rect.first, rect.second);
cout << "Length: " << rect.first << ", Width: " << rect.second
<< " -> Area: " << area << endl;
}
return 0;
}
Using a vector of pairs, this program efficiently handles multiple rectangles. Beginners can see how loops, vectors, and functions work together to process datasets, preparing them for more complex programming challenges.
Frequently Asked Questions (FAQ)
Q1: Can I use integers instead of double for length and width?
Yes, but using double
allows for fractional values and more accurate area calculations.
Q2: Why use functions for area calculation?
Functions make your code reusable, organized, and easier to debug, especially when calculating areas for multiple rectangles.
Q3: How can I calculate areas for user-defined multiple rectangles?
You can combine vectors with loops to input dimensions and calculate areas dynamically, similar to Program 4.
Q4: Can this method be used for other shapes?
Absolutely! The same logic can be adapted for squares, triangles, circles, and more by changing the calculation formula.
Conclusion
Finding the area of a rectangle in C++ is a practical exercise for beginners to strengthen their understanding of variables, input/output, arithmetic, loops, vectors, and functions. Starting with user input, moving to predefined values, and finally using functions and vectors shows how programming scales from simple to structured, reusable code. Practicing these methods builds confidence and prepares learners for more advanced programming challenges.
Additional & References
To continue learning, beginners should experiment with functions, vectors, and loops for geometry-based calculations. Practicing multiple approaches helps reinforce programming logic and real-world applications.
- GeeksforGeeks: Area And Perimeter of Rectangle in C++ – Beginner-friendly tutorial with examples.
- C++ Vectors – Official documentation on vector usage.
- Programiz C++ Tutorials – Tutorials covering loops, functions, and I/O operations.
- LearnCpp: C++ Functions – Guide on creating and using functions efficiently.
- LearnCpp: C++ Input/Output – Guide to handling user input and output effectively.