In this article, we are going to understand and implement a simple C program, to find the perimeter of the rectangle. To calculate the perimeter of a rectangle is very easy using a C program, provided that you are already familiar with the simple formula to calculate the perimeter of a rectangle, and you know how to write C programs.
C Program to Find Perimeter of a Rectangle
In the given video, you can find an easy explanation along with the simple C program to find the perimeter of a rectangle.
The perimeter of a rectangle can be simply calculated as the sum of all the sides of the rectangle. Here is the formula for calculating the perimeter of a rectangle.
P=2(l+w)
Here, l is the length, w is the width, and P is the perimeter of the rectangle. So, having the length and the width, you can easily calculate the perimeter of the rectangle.
Here is the C program to calculate the perimeter of the rectangle –
#include <stdio.h>
int main() {
float length, width, perimeter;
// Input the length and width of the rectangle
printf("Enter the length of the rectangle: ");
scanf("%f", &length);
printf("Enter the width of the rectangle: ");
scanf("%f", &width);
// Calculate the perimeter of the rectangle
perimeter = 2 * (length + width);
// Print the perimeter
printf("Perimeter of the rectangle: %.2fn", perimeter);
return 0;
}
Output 1 –
Enter the length of the rectangle: 5
Enter the width of the rectangle: 2
Perimeter of the rectangle: 14.00
Output 2 –
Enter the length of the rectangle: 15
Enter the width of the rectangle: 6
Perimeter of the rectangle: 42.00