Finding the perimeter and area of a rectangle is a fundamental geometric calculation often needed in various applications, such as graphics, layout design, and scientific calculations. Understanding how to implement these calculations in C is beneficial for beginners.
Purpose and Functionality
- Perimeter of a Rectangle: The perimeter is the total distance around the rectangle's boundary. It is calculated as:
Perimeter=2×(length+width)
- Area of a Rectangle: The area represents the space occupied by the rectangle. It is calculated as:
Area=length×width
Implementation in C
The following C program demonstrates how to find the perimeter and area of a rectangle. The program prompts the user to enter the rectangle's length and width, performs the calculations, and then displays the results.
Example:
#include <stdio.h>
void main() {
/* Variable Declaration. */
float lnth, wdth, perimeter, area;
/* Taking input of the length of the rectangle from the user */
printf("Enter the length of the Rectangle:\n");
scanf("%f", & lnth);
/* Taking input of the width of the rectangle from the user */
printf("Enter the width of the Rectangle:\n");
scanf("%f", & wdth);
/* Calculate perimeter of the rectangle */
perimeter = 2 * (lnth + wdth);
printf("Perimeter of the Rectangle: %0.4f\n", perimeter);
/* Calculate area of the rectangle */
area = lnth * wdth;
printf("Area of the Rectangle: %0.4f\n", area);
}
When you run the above program, it asks for the length and width of the rectangle. After you input these values, it displays the perimeter and area.
Program Output:
Enter the length of the Rectangle:
50
Enter the width of the Rectangle:
80
Perimeter of the Rectangle: 260.0000
Area of the Rectangle: 4000.0000
Conclusion
In this tutorial, you learned how to calculate the perimeter and area of a rectangle using C programs. The example illustrates basic arithmetic operations and shows how to interact with users by taking input and displaying the result. This fundamental knowledge is essential to advance in programming and solve more complex problems.