C++
#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
Area and Perimeter of Rectangle in C++
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.
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
Program Logic
- Read length and width from user
- Calculate area = length × width
- Calculate perimeter = 2 × (length + width)
- Display both results
Key Takeaways
1
Area = length × width (space inside rectangle)
2
Perimeter = 2 × (length + width) (distance around)
3
Use
float to handle decimal measurements4
Area measured in square units, perimeter in linear units
5
These formulas are fundamental in geometry and real-world applications