w3resource

C++ Exercises: Find the Area and Perimeter of a Rectangle

C++ Basic: Exercise-17 with Solution

Write a C++ program to find the Area and Perimeter of a Rectangle.

Visual Presentation:

C++ Exercises: Find the Area and Perimeter of a Rectangle

Sample Solution:

C++ Code :

#include <iostream> // Including the input-output stream header file

using namespace std; // Using the standard namespace

int main() // Start of the main function
{
    int width, lngth, area, peri; // Declaring integer variables width, length, area, and perimeter

    cout << "\n\n Find the Area and Perimeter of a Rectangle :\n"; // Outputting a message indicating calculation of rectangle area and perimeter
    cout << "-------------------------------------------------\n"; // Outputting a separator line

    cout << " Input the length of the rectangle : "; // Prompting the user to input the length of the rectangle
    cin >> lngth; // Taking input for the length from the user

    cout << " Input the width of the rectangle : "; // Prompting the user to input the width of the rectangle
    cin >> width; // Taking input for the width from the user

    area = (lngth * width); // Calculating the area of the rectangle: length * width
    peri = 2 * (lngth + width); // Calculating the perimeter of the rectangle: 2 * (length + width)

    cout << " The area of the rectangle is : "<< area << endl; // Displaying the calculated area of the rectangle
    cout << " The perimeter of the rectangle is : "<< peri << endl; // Displaying the calculated perimeter of the rectangle
    cout << endl; // Outputting a blank line for better readability

    return 0; // Returning 0 to indicate successful program execution
} // End of the main function

Sample Output:

 Find the Area and Perimeter of a Rectangle :                          
-------------------------------------------------                      
 Input the length of the rectangle : 10                                
 Input the width of the rectangle : 15                                 
 The area of the rectangle is : 150                                    
 The perimeter of the rectangle is : 50 

Flowchart:

Flowchart: Find the Area and Perimeter of a Rectangle

C++ Code Editor:

Previous: Write a program in C++ to calculate the volume of a cylinder.
Next: Write a program in C++ to find the area of any triangle using Heron's Formula.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.