102k views
2 votes
c++ Write a program to calculate the area and perimeter of a number of rectangles. You will use a for loop in this program.

User Oriol Prat
by
8.3k points

1 Answer

3 votes

Here is a sample program in C++ to calculate the area and perimeter of a number of rectangles using a for loop:

```

#include <iostream>

using namespace std;

int main() {

int num_rectangles;

cout << "Enter the number of rectangles: ";

cin >> num_rectangles;

for (int i = 1; i <= num_rectangles; i++) {

float length, width, area, perimeter;

cout << "Enter the length and width of rectangle " << i << ": ";

cin >> length >> width;

area = length * width;

perimeter = 2 * (length + width);

cout << "The area of rectangle " << i << " is: " << area << endl;

cout << "The perimeter of rectangle " << i << " is: " << perimeter << endl;

}

return 0;

}

```

Step-by-step explanation:

- The program first asks the user to enter the number of rectangles they want to calculate the area and perimeter for.

- It then uses a for loop to iterate through each rectangle, from 1 to the user-specified number of rectangles.

- For each rectangle, the program asks the user to enter the length and width, and calculates the area and perimeter using the formulas `area = length * width` and `perimeter = 2 * (length + width)`.

- Finally, the program displays the calculated area and perimeter for each rectangle.

User Red Fx
by
7.5k points

No related questions found