C++ program to enter length and breadth of a rectangle and find its perimeter
ADVERTISEMENTS
In this article, you will learn how to find the perimeter of a rectangle in the c++ language.
You should have knowledge of the following topics in c++ programming.
- C++
main()
function - C++
cin
object - C++
cout
object
Formula
This is the standard formula to calculate the perimeter of a rectangle.
P = 2(l + w)
Where
P
= perimeter
l
= length of the rectangle
w
= width of the rectangle
Source Code
// C++ program to find the perimeter of a rectangle
#include <iostream>
using namespace std;
int main() {
float l, w, p;
cout << "Enter the length & width of the rectangle::\n";
cin >> l;
cin >> w;
/* Calculate perimeter of rectangle */
p = 2 * (l + w);
/* Print output */
cout << "\nThe Perimeter of rectangle = " << p << " units";
return 0;
}
Output
Enter the length & width of the rectangle::
5
7
The perimeter of rectangle = 24 units
Explanation
In this program, we have taken two inputs from the user rectangle's width 5
and rectangle's height 7
.
Then used the standard formula P = 2(l + w)
to calculate the perimeter of a rectangle then it will return the 24 units
the perimeter of the rectangle.