Howdy readers, today you will learn how to write a C++ program to find the perimeter of a rectangle.
The perimeter of a rectangle is the total length or distance of its boundary on all sides. It is twice the sum of its length and width, and it is calculated with the help of a formula: Perimeter = 2(length + width).
This tutorial computes the perimeter of a rectangle using the following methods:
- Using Standard Method
- Using Function
So, without any delay, let’s begin this tutorial.
C++ Program to Find Perimeter of a Rectangle
C++ Program
// C++ Program to Find Perimeter of a Rectangle #include <iostream> using namespace std; int main(){ float length, breadth, perimeter; // Taking input cout << "Enter the length of the rectangle: "; cin >> length; cout << "Enter the breadth of the rectangle: "; cin >> breadth; // Perimeter of a rectangle perimeter = 2 * (length + breadth); // Display perimeter cout << "The perimeter of the rectangle is: " << perimeter << endl; return 0; }
Output
Enter the length of the rectangle: 15 Enter the breadth of the rectangle: 18 The perimeter of the rectangle is: 66

Explanation
float length, breadth, perimeter;
We have declared three float data type variables named length
, breadth
and perimeter
.
cout << "Enter the length of the rectangle: "; cin >> length; cout << "Enter the breadth of the rectangle: "; cin >> breadth;
The length and breadth of the rectangle are entered by the user. The entered values get stored in the length
and breadth
named variables.
perimeter = 2 * (length + breadth);
The perimeter of the rectangle is calculated using the formula: Perimeter = 2(length + width).
cout << "Perimeter of the rectangle is: " << perimeter << endl;
At last, the perimeter of the rectangle is printed on the screen using the cout
statement.
C++ Program to Find Perimeter of a Rectangle Using Function
C++ Program
// C++ Program to Find Perimeter of a Rectangle Using Function #include <iostream> using namespace std; float periRect(float a, float b){ return 2 * (a + b); } int main(){ float l, b, perimeter; // Taking input cout << "Enter the length of the rectangle: "; cin >> l; cout << "Enter the breadth of the rectangle: "; cin >> b; // Calling out function perimeter = periRect(l, b); // Display output cout << "Perimeter = " << perimeter; return 0; }
Output
Enter the length of the rectangle: 7 Enter the breadth of the rectangle: 5 Perimeter = 24

Explanation
float periRect(float a, float b){ return 2 * (a + b); }
In this program, we have declared and defined a function named periRect
which passes the dimensions of the rectangle through arguments and returns the perimeter.
perimeter = periRect(l, b);
Then, we call out the custom function in the main function. This gives us the value of the perimeter. This value gets stored in the perimeter
named variable.
Conclusion
Today you learned how to write a C++ program to find the perimeter of a rectangle.
If you have any questions related to the program, comment down your question in the comment section.
Thanks for reading.
Happy Coding!!
Also Read:
- C++ Program to Print First 10 Even Natural Numbers
- C++ Program to Print First 10 Odd Natural Numbers
- C++ Program to Calculate Square of a Number
- C++ Program to Find Largest of Three Numbers
- C++ Program to Find Sum and Average of Two Numbers