C Program To Find The Area And Perimeter Of A Rectangle
This article explores how to calculate the area and perimeter of a rectangle using the C programming language. In this article, you will learn to implement a simple C program that takes length and width as input and provides accurate calculations.
Problem Statement
A common mathematical task involves determining the area and perimeter of a rectangle. The area represents the space enclosed within the rectangle, while the perimeter is the total length of its boundary. Accurately calculating these values is crucial in various real-world applications.
Example
Consider a rectangle with a length of 10 units and a width of 5 units. The expected output for this rectangle would be:
- Area: 50 square units
- Perimeter: 30 units
Background & Knowledge Prerequisites
To understand and implement the C program for this task, readers should have a basic understanding of:
- Variables: How to declare and use variables to store data (e.g.,
floatfor decimal numbers). - Input/Output Operations: Using
printf()to display output andscanf()to read user input. - Arithmetic Operators: Performing basic calculations like addition (
+), multiplication (*). - Basic Program Structure: The
main()function and including header files likestdio.h.
Use Cases or Case Studies
Calculating the area and perimeter of a rectangle has numerous practical applications:
- Construction and Architecture: Determining the amount of material needed for flooring, painting walls, or fencing a rectangular plot of land.
- Gardening and Landscaping: Planning garden layouts, calculating the size of flowerbeds, or determining the length of edging required.
- Manufacturing and Design: Estimating the surface area of components or the length of material needed to form rectangular parts.
- Computer Graphics and UI Design: Calculating the dimensions and layout of screen elements, windows, or image sizes.
- Education: As a fundamental exercise in learning programming and basic geometry.
Solution Approaches
Basic Calculation with User Input
This approach prompts the user to enter the length and width of the rectangle, then computes and displays the area and perimeter using standard mathematical formulas.
// Calculate Area and Perimeter of a Rectangle
#include <stdio.h> // Required for input/output functions
int main() {
// Step 1: Declare variables to store length, width, area, and perimeter.
// Using float allows for non-integer dimensions.
float length;
float width;
float area;
float perimeter;
// Step 2: Prompt the user to enter the length of the rectangle.
printf("Enter the length of the rectangle: ");
// Step 3: Read the length value from the user.
scanf("%f", &length);
// Step 4: Prompt the user to enter the width of the rectangle.
printf("Enter the width of the rectangle: ");
// Step 5: Read the width value from the user.
scanf("%f", &width);
// Step 6: Calculate the area of the rectangle.
// Formula: Area = length * width
area = length * width;
// Step 7: Calculate the perimeter of the rectangle.
// Formula: Perimeter = 2 * (length + width)
perimeter = 2 * (length + width);
// Step 8: Display the calculated area and perimeter to the user.
// %.2f formats the float to two decimal places.
printf("\\nArea of the rectangle: %.2f square units\\n", area);
printf("Perimeter of the rectangle: %.2f units\\n", perimeter);
return 0; // Indicate successful program execution
}
Sample Output
Enter the length of the rectangle: 12.5
Enter the width of the rectangle: 8.2
Area of the rectangle: 102.50 square units
Perimeter of the rectangle: 41.40 units
Stepwise Explanation
- Include Header: The
#includeline brings in the standard input/output library, necessary for functions likeprintf()andscanf(). - Declare Variables: Inside
main(), fourfloatvariables (length,width,area,perimeter) are declared.floatis chosen to handle decimal values for dimensions. - Get Length Input: The program uses
printf()to ask the user for the rectangle's length andscanf("%f", &length);to read the floating-point value entered by the user and store it in thelengthvariable. The&beforelengthis crucial as it passes the memory address of the variable. - Get Width Input: Similarly, the program prompts for and reads the
width. - Calculate Area: The
areais computed using the formulalength * widthand the result is stored in theareavariable. - Calculate Perimeter: The
perimeteris calculated using the formula2 * (length + width)and stored in theperimetervariable. - Display Results: Finally,
printf()statements are used to display the calculatedareaandperimeter. The%.2fformat specifier ensures that the floating-point numbers are displayed with two decimal places, making the output cleaner. - Return 0:
return 0;indicates that the program executed successfully.
Conclusion
This C program provides a straightforward and efficient method to calculate the area and perimeter of a rectangle. By taking user input for length and width, it performs the necessary arithmetic operations and presents the results clearly. This fundamental example showcases basic input/output, variable handling, and arithmetic in C.
Summary
- A rectangle's area is calculated by multiplying its length by its width (Area = L \* W).
- The perimeter is found by adding all four sides, or using the formula: 2 \* (L + W).
- The C program utilizes
printf()for output andscanf()for user input to gather dimensions. -
floatdata type is used for variables to accommodate non-integer dimensions. - The
stdio.hheader file is essential for standard input/output functions.