How to Write C Program to Calculate the Area Of a Rectangle and the Perimeter of a Rectangle with an example? Before we step into the C program, let’s see the definitions and formulas to calculate the Area and Perimeter Of a Rectangle.
Area of a Rectangle
If we know the width and height, we can calculate the area of a rectangle using the below formula.
Area = Width * Height
Perimeter is the distance around the edges. We can calculate the perimeter of a rectangle using the below formula.
Perimeter = 2 * (Width + Height)
C Program to Calculate Area of a Rectangle and Perimeter of a Rectangle
This C Program allows the user to enter the width and height of the rectangle. Using those values, we will calculate the Area of a rectangle and the perimeter of a rectangle.
#include<stdio.h> int main() { float width, height, Area, Perimeter; printf ("\n Please Enter the Width and Height of the rectangle \n"); scanf (" %f %f ",&width, &height); Area = width * height; Perimeter = 2 *(width + height); printf("\n Area of a rectangle is: %.2f", Area); printf("\n Perimeter of a rectangle is: %.2f", Perimeter); return 0; }
The following statements in this program will allow the User to enter the Width and Height of a rectangle.
printf ("\n Please Enter the Width and Height of the rectangle \n"); scanf (" %f %f ",&width, &height);
Next, we calculate the area as per the formula.
Area = width * height
In the next line of the C Program to Calculate Area of a Rectangle example, we are calculating the Perimeter of the rectangle.
Perimeter = 2 *(width + height)
The following Programming printf statements will help us to print the Perimeter and Area of a rectangle.
printf("\n Area of a rectangle is: %.2f", Area); printf("\n Perimeter of a rectangle is: %.2f", Perimeter);