Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

C++ Exercises: Print the area and perimeter of a rectangle

C++ Basic: Exercise-40 with Solution

Write a program in C++ to print the area and perimeter of a rectangle.

Pictorial Presentation:

C++ Exercises: Print the area and perimeter of a rectangle

Sample Solution:

C++ Code :

#include <iostream>
using namespace std;
 
int main()
{
float ar,peri,width,height=0;
    cout << "\n\n Print the area and perimeter of a rectangle:\n";
	cout << "----------------------------------------------\n";
	cout << " Input the width of the rectangle: ";
	cin>> width;
	cout << " Input the height of the rectangle: ";
	cin>> height;	
	peri=2 * (width+height);
	ar= height * width;
	cout<<"The area of the rectangle is: "<<ar<<"\n";
	cout<<"The perimeter of the rectangle is: "<<peri<<"\n";
	
}

Sample Output:

  Print the area and perimeter of a rectangle:                          
----------------------------------------------                         
 Input the width of the rectangle: 8.5                                 
 Input the height of the rectangle: 5.6                                
The area of the rectangle is: 47.6                                     
The perimeter of the rectangle is: 28.2

Flowchart:

Flowchart: Print the area and perimeter of a rectangle

C++ Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a program in C++ to print the specified pattern.
Next: Write a program in C++ to print an American flag on the screen.

What is the difficulty level of this exercise?