C Program To Calculate The Area And Perimeter Of A Rectangle
ADVERTISEMENTS
Introduction
Understanding basic geometric calculations is a fundamental skill in programming, especially when dealing with shapes and measurements. In this article, you will learn how to write a C program to calculate the area and perimeter of a rectangle, a common task in various applications.Problem Statement
The challenge is to create a C program that can accurately determine the area and perimeter of a rectangle. This requires taking two inputs: the length and the width of the rectangle, and then applying the standard mathematical formulas to compute and display the results. This is a foundational problem that helps reinforce understanding of variables, input/output operations, and basic arithmetic in C programming.Example
If a rectangle has a length of10 units and a width of 5 units, the program should produce an output similar to this:
Enter the length of the rectangle: 10
Enter the width of the rectangle: 5
Area of the rectangle: 50.00 square units
Perimeter of the rectangle: 30.00 units
Background & Knowledge Prerequisites
To understand this article, readers should be familiar with the following C programming concepts:- Variables: Declaring and using variables to store data.
- Data Types: Understanding basic data types like
int(for whole numbers) andfloatordouble(for decimal numbers). - Input/Output Operations: Using
printf()for displaying output andscanf()for taking user input. - Arithmetic Operators: Performing basic mathematical operations such as addition (
+) and multiplication (*). - Header Files: The purpose of
#includefor standard input/output functions.
Use Cases or Case Studies
Calculating the area and perimeter of a rectangle is a basic operation with numerous practical applications:- Architectural Design: Estimating the amount of flooring, paint, or fencing needed for rectangular rooms or plots.
- Game Development: Determining collision boundaries for rectangular objects or calculating the visible area on a screen.
- Inventory Management: Calculating the surface area of rectangular packages for storage optimization or shipping costs.
- Computer Graphics: Drawing and manipulating rectangular shapes on a screen, where dimensions often relate to pixel areas.
- Basic Geometry Education: Serving as a simple example to teach fundamental geometric principles and their implementation in programming.
Solution Approaches
Standard Calculation with User Input
This approach involves prompting the user to enter the length and width of the rectangle, reading these values, and then applying the standard formulas for area and perimeter.One-line summary: Obtain rectangle dimensions from user input, then compute and display area (length \* width) and perimeter (2 \* (length + width)).
// Calculate Area and Perimeter of a Rectangle
#include <stdio.h>
int main() {
// Step 1: Declare variables to store length, width, area, and perimeter.
// Using float for dimensions allows for non-integer values.
float length, width, area, 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 entered by 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 entered by 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.
// "%.2f" formats the float to two decimal places.
printf("Area of the rectangle: %.2f square units\\n", area);
// Step 9: Display the calculated perimeter.
printf("Perimeter of the rectangle: %.2f units\\n", perimeter);
return 0;
}
Sample Output:
Enter the length of the rectangle: 12.5
Enter the width of the rectangle: 8
Area of the rectangle: 100.00 square units
Perimeter of the rectangle: 41.00 units
Stepwise Explanation:
- Variable Declaration:
-
float length, width, area, perimeter;declares four variables of typefloat. Usingfloatallows the program to handle dimensions that are not whole numbers.
- Input Length:
-
printf("Enter the length of the rectangle: ");displays a message on the console, asking the user for the rectangle's length. -
scanf("%f", &length);reads the floating-point value entered by the user from the keyboard and stores it in thelengthvariable. The&operator provides the memory address of thelengthvariable.
- Input Width:
- Similarly, the program prompts for the width using
printfand reads the input into thewidthvariable usingscanf.
- Calculate Area:
-
area = length * width;computes the area by multiplying thelengthandwidthvalues and assigns the result to theareavariable.
- Calculate Perimeter:
-
perimeter = 2 * (length + width);calculates the perimeter using the formula and stores the result in theperimetervariable. Parentheses ensure thatlength + widthis evaluated first.
- Display Results:
-
printf("Area of the rectangle: %.2f square units\n", area);prints the calculated area to the console. The%.2fformat specifier ensures that thefloatvalue is displayed with exactly two decimal places.\ninserts a newline character, moving the cursor to the next line. -
printf("Perimeter of the rectangle: %.2f units\n", perimeter);does the same for the perimeter.
- Return 0:
-
return 0;indicates that themainfunction has executed successfully.
Conclusion
This article demonstrated how to implement a C program to calculate the area and perimeter of a rectangle. By leveraging fundamental C concepts like variable declaration, user input (scanf), arithmetic operations, and output display (printf), we can create simple yet powerful tools for geometric computations. This basic program serves as an excellent foundation for understanding more complex calculations and object-oriented geometry in C and other programming languages.
Summary
- Problem: Calculate the area and perimeter of a rectangle given its length and width.
- Inputs: Length and width of the rectangle (float values).
- Formulas:
- Area = Length \* Width
- Perimeter = 2 \* (Length + Width)
- C Implementation:
- Uses
floatdata type for dimensions. - Employs
printf()for user prompts and displaying results. - Utilizes
scanf()to read user input. - Applies standard arithmetic operators (
*,+). - Outputs results formatted to two decimal places using
%.2f.