C++ Program to find the area and perimeter of a rectangle using function.


January 30, 2023, Learn eTutorial
1794

In this C++ program, you will learn and get code on finding the area and perimeter of a rectangle. Here we are using the approach using class and object to create the program.

How do we find the area and perimeter of a rectangle?

The area of a rectangle can be defined as the space or area occupied by the four sides which make a enclosed shape of rectangle. Area of a rectangle shape can be calculated by multiplying the length and breadth of it.

Area of rectangle A = Length x Breadth

The perimeter of a rectangle is the total distance covered by its boundaries or sides. SO the perimeter of the rectangle can be calculated as the sum of four sides of it. In case of rectangle 2 sides are equal so we multiply the sum of length and breadth with 2.

Perimeter P = 2 * ( a + b )

How to calculate the area and perimeter of a rectangle using the function in C++?

Ask the user to enter the value of the length and breadth of the rectangle. Read the value into the float type variables l and b. define a function to compute the area of the rectangle. 
float areaofrectangle(float l, float b)
{
    return (l*b);
}

Where,

  • Float – return type
  • areaofrectangle – function name
  • float l, float b – parameters

The function will return the value l * b. Get the value into the variable area = areaofrectangle(l, b) and display the result.


Define another function to compute the perimeter of the rectangle with the value entered into the variables l and b.
float perimeterofrectangle(float l, float b)
{
    return (2 * (l + b));
}

where,

  • float – return type
  • perimeterofrectangle – function name
  • float l, float b – parameters

The function will return the value, get the value into the variable perimeter = perimeterofrectangle(l, b) and Display the result

Algorithm

Step 1:  Call the header file iostream.

Step 2: Use the namespace std.

Step 3: Declare two functions for compute the area and the perimeter of the rectangle;

              float areaofrectangle( float l, float b);

              float perimeterofrectangle( float l, float b);

Step 4: Open the main function; int main().

Step 5: Declare  float type variables l, b, area, perimeter;

Step 6: Ask the user to enter the value of length and breadth of the rectangle.

Step 7: Get the value of length to the variable l and breadth to b;

Step 8: Call step: 12 and get the value to the variable area;

Step 9: Display the result

Step 10: Call step: 13 and get the value to the variable perimeter;

Step 11: Display the result;

Step 12: Define the function float areaofrectangle( float l, float b);

               float areaofrectangle(float l, float b) { return (l*b); }

Step 13: Define the function float perimeterofrectangle( float l, float b);

               float perimeterofrectangle(float l, float b)
          {  return (2 * (l + b));  }

Step 14: Exit

C++ Source Code

                                          #include<iostream>
using namespace std;
float areaofrectangle(float, float);
float perimeterofrectangle(float, float);
int main()
{
    float l, b, area, perimeter;
    cout<<"Enter Length of Rectangle: ";
    cin>>l;
    cout<<"Enter Breadth of Rectangle: ";
    cin>>b;
    area = areaofrectangle(l, b);
    perimeter = perimeterofrectangle(l, b);
    cout<<"\nArea = "<<area;
    cout<<endl;
    cout<<"\nPerimeter = "<<perimeter;
    cout<<endl;
    return 0;
}
float areaofrectangle(float l, float b)
{
    return (l*b);
}
float perimeterofRectangle(float l, float b)
{
    return (2*(l+b));
}
                                      

OUTPUT

234