C Program to Calculate Area and Perimeter of a Rectangle
Learning how to calculate the area and perimeter of geometric shapes is one of the most common beginner exercises in programming. It helps new programmers understand how mathematical formulas can be translated into code. In this tutorial, we will write a simple C program that calculates both the area and the perimeter of a rectangle using user input.
This program demonstrates several important programming concepts including variables, user input, arithmetic operators, formatted output, and basic program structure. By the end of this tutorial, you will understand not only how to write the program but also why each part of the code works.
What is a Rectangle?
A rectangle is a four-sided geometric shape where opposite sides are equal and all angles are 90 degrees. It has two main dimensions: length and breadth (sometimes called width). Many real-world objects such as books, tables, screens, and doors follow a rectangular shape.
When working with rectangles, two common calculations are used in mathematics and engineering: the area and the perimeter.
Mathematical Formulas
To calculate the area and perimeter of a rectangle, we use the following formulas:
- **Area** = Length × Breadth
- **Perimeter** = 2 × (Length + Breadth)
The area tells us how much surface space the rectangle covers. For example, if a floor is shaped like a rectangle, the area tells us how many square units of tiles are needed to cover it.
The perimeter tells us the total distance around the rectangle. This is useful when calculating fencing required for land or framing around a rectangular object.
Program Logic
Before writing the code, it is helpful to understand the logical steps the program will follow.
- Declare variables for length, breadth, area, and perimeter.
- Ask the user to enter the length of the rectangle.
- Store the value using the scanf function.
- Ask the user to enter the breadth of the rectangle.
- Calculate the area using the formula: area = length * breadth.
- Calculate the perimeter using the formula: perimeter = 2 * (length + breadth).
- Display the results to the user.
C Program
#include <stdio.h>
int main() {
float length, breadth, area, perimeter;
printf("Enter the length of the rectangle: ");
scanf("%f", &length);
printf("Enter the breadth of the rectangle: ");
scanf("%f", &breadth);
area = length * breadth;
perimeter = 2 * (length + breadth);
printf("\n--- Results ---\n");
printf("Area of Rectangle: %.2f\n", area);
printf("Perimeter of Rectangle: %.2f\n", perimeter);
return 0;
}
Sample Output
Enter the length of the rectangle: 10.5 Enter the breadth of the rectangle: 5.2 --- Results --- Area of Rectangle: 54.60 Perimeter of Rectangle: 31.40
Step-by-Step Code Explanation
Let's understand how each part of the program works.
- `#include
` includes the standard input-output library so we can use functions like printf and scanf. - `int main()` is the starting point of every C program.
- `float length, breadth, area, perimeter;` declares variables that store decimal values.
- `printf()` displays text on the screen to guide the user.
- `scanf()` reads the user's input and stores it into variables.
- The area is calculated using multiplication: length * breadth.
- The perimeter uses parentheses to ensure addition happens before multiplication.
- `%.2f` ensures the output is displayed with two decimal places.
Example Calculation
Suppose the user enters the following values:
- Length = 10.5
- Breadth = 5.2
Area Calculation:
Area = 10.5 × 5.2 Area = 54.6
Perimeter Calculation:
Perimeter = 2 × (10.5 + 5.2) Perimeter = 2 × 15.7 Perimeter = 31.4
Dry Run of the Program
A dry run helps us understand how the program executes internally.
- User enters length = 10.5
- User enters breadth = 5.2
- Area becomes 10.5 * 5.2 = 54.6
- Perimeter becomes 2 * (10.5 + 5.2) = 31.4
- The program prints the results.
Common Mistakes Beginners Make
- Forgetting to include the ampersand (&) in scanf.
- Using integer variables instead of float when decimal values are needed.
- Writing the perimeter formula incorrectly as 2 * length + breadth.
- Forgetting semicolons at the end of statements.
Real-World Applications
Although this program is simple, the same logic is used in many real-world applications.
- Construction projects to calculate floor space.
- Interior design to determine carpet or tile coverage.
- Agriculture to measure field area.
- Manufacturing to calculate material requirements.
- Computer graphics and game development.
Practice Exercises
- Modify the program to calculate the diagonal of a rectangle.
- Create a program that handles square shapes automatically if length equals breadth.
- Allow the user to calculate the area of multiple rectangles.
- Convert the program into a function-based version.
Conclusion
In this tutorial, we learned how to write a C program that calculates the area and perimeter of a rectangle using user input. This example demonstrates how mathematical formulas can be implemented using simple arithmetic operators in C. Understanding these basics helps build the foundation for more advanced programming concepts such as functions, structures, and graphical programming.
Codecrown