In this article, you will learn and get code on printing the area and perimeter of a rectangle based on length and breadth entered by user at run-time in C++ programming. Here are the list of approaches used to find area and perimeter of a rectangle:
The formula to calculate area of a rectangle is given below:
area = len*bre
Here, len indicates length and bre indicates breadth of rectangle.
To calculate perimeter of rectangle, use following formula:
perimeter = 2*(len+bre)
Here also, len and bre indicates to length and breadth of the rectangle. Now let's move on to the program.
The question is, write a program in C++ to find and print area of a rectangle. The program given below is the answer of this question:
// C++ Program to Find Area of Rectangle // ----codescracker.com---- #include<iostream> using namespace std; int main() { float len, bre, area; cout<<"Enter Length of Rectangle: "; cin>>len; cout<<"Enter Breadth of Rectangle: "; cin>>bre; area = len*bre; cout<<"\nArea = "<<area; cout<<endl; return 0; }
This program was build and run under Code::Blocks IDE. Here is its sample run:
Now enter the length and breadth of rectangle, and press ENTER
key to see the area based on given
length and breadth of rectangle as shown in the output given below:
When user enters length, it gets initialized to len, and when enters breadth, it gets initialized to bre. Using the formula, len*bre gets initialized to area variable, that holds the area of rectangle. Therefore just print its value as output.
Let's create the same purpose program using a user defined function, areaOfRectangle(). This function takes two arguments and returns a single value. The two arguments are length and breadth, whereas its return value will be the area of rectangle. To learn more about function, you can follow its separate tutorial.
// Find Area of Rectangle using Function // ----codescracker.com---- #include<iostream> using namespace std; float areaOfRectangle(float, float); int main() { float len, bre, area; cout<<"Enter Length of Rectangle: "; cin>>len; cout<<"Enter Breadth of Rectangle: "; cin>>bre; area = areaOfRectangle(len, bre); cout<<"\nArea = "<<area; cout<<endl; return 0; } float areaOfRectangle(float len, float bre) { return (len*bre); }
Now let's find and print the perimeter of a rectangle without using function.
// C++ Program to Find Perimeter of Rectangle // ----codescracker.com---- #include<iostream> using namespace std; int main() { float len, bre, per; cout<<"Enter Length and Breadth of Rectangle: "; cin>>len>>bre; per = 2*(len+bre); cout<<"\nPerimeter = "<<per; cout<<endl; return 0; }
Here is the sample run of above program:
Now supply the value of length and breadth of a ractangle to find and prints its perimeter as shown in the output given below
Here is another program that also find and prints perimeter of rectangle using user-defined function.
// Find Perimeter of Rectangle using Function // ----codescracker.com---- #include<iostream> using namespace std; float perOfRectangle(float, float); int main() { float len, bre; cout<<"Enter Length and Breadth of Rectangle: "; cin>>len>>bre; cout<<"\nPerimeter = "<<perOfRectangle(len, bre); cout<<endl; return 0; } float perOfRectangle(float len, float bre) { return (2*(len+bre)); }
Now let's combine both and create a single program using class to find area and perimeter of a rectangle. For more detail about classes and objects, you can refer to its separate tutorial.
// Find Area and Perimeter of Rectangle using Class // ----codescracker.com---- #include<iostream> using namespace std; class CodesCracker { private: float len, bre; public: void getData(); float areaOfRect(); float periOfRect(); }; void CodesCracker::getData() { cout<<"Enter Length and Breadth of Rectangle: "; cin>>len>>bre; } float CodesCracker::areaOfRect() { return (len*bre); } float CodesCracker::periOfRect() { return (2*(len+bre)); } int main() { CodesCracker c; float area, per; c.getData(); area = c.areaOfRect(); per = c.periOfRect(); cout<<"\nArea = "<<area; cout<<"\nPerimeter = "<<per; cout<<endl; return 0; }
Here is its sample run with user input of 5 as length and 3 as breadth: