C Program to Calculate Area and Perimeter of Rectangle

Howdy readers, today you will learn how to write a program to calculate the area and perimeter of a rectangle using C Programming language.

C Program to Calculate Area and Perimeter of Rectangle

A rectangle is a closed figure of four sides having two equal pairs of opposite sides and the angle formed by adjacent sides is 90 degrees.

The formula for the area, ‘A’ of a rectangle whose length and width are ‘L’ and ‘B’ respectively is the product ‘L x B’.

  • Area of a Rectangle = (Length x Breadth) square units

The formula for the perimeter of a rectangle is:

  • Perimeter of a Rectangle = 2(Length + Breadth) units

We will use the above formula in this tutorial to calculate the area and perimeter of the rectangle.

So, without any delay, let’s begin this tutorial.

Table of Contents

C Program to Calculate Area and Perimeter of Rectangle

C Program

// C Program to Calculate Area and Perimeter of Rectangle
#include <stdio.h>
int main(){
    float length, breadth, area, perimeter;
    
    // Asking for input
    printf("Enter the length of the rectangle: ");
    scanf("%f", &length);
    printf("Enter the breadth of the rectangle: ");
    scanf("%f", &breadth);
    
    // Calculating area of the rectangle
    area = length * breadth;
    
    // Calculating perimeter of the rectangle
    perimeter = 2 * (length + breadth);
    
    // Printing result
    printf("Area of the rectangle is: %.2f \n", area);
    printf("Perimeter of the rectangle is: %.2f", perimeter);
    
    return 0;
}

Output

Enter the length of the rectangle: 4.6
Enter the breadth of the rectangle: 5.2
Area of the rectangle is: 23.92 
Perimeter of the rectangle is: 19.60
C Program to Calculate Area and Perimeter of Rectangle

Explanation

    float length, breadth, area, perimeter;

We have declared four floating data type variables named length, breadth, area and perimeter.

    // Asking for input
    printf("Enter the length of the rectangle: ");
    scanf("%f", &length);
    printf("Enter the breadth of the rectangle: ");
    scanf("%f", &breadth);

The user is asked to enter the length and breadth of the rectangle.

    // Calculating area of the rectangle
    area = length * breadth;

We calculate the area of the rectangle using the formula, area = length x breadth.

    // Calculating perimeter of the rectangle
    perimeter = 2 * (length + breadth);

Similarly, the perimeter of the rectangle is calculated using the formula, perimeter = 2 x (length + breadth).

    // Printing result
    printf("Area of the rectangle is: %.2f \n", area);
    printf("Perimeter of the rectangle is: %.2f", perimeter);

At the end, the result is displayed on the screen using printf() function.

Conclusion

I hope after reading this tutorial, you understand how to write a program to calculate area and perimeter of the rectangle using C Programming language.

If you face any difficulty while understanding this program, then let us know in the comment section. We will be delighted to solve your queries.

Leave a Comment