If you’ve ever measured a room to fit a carpet or frame, you've worked with rectangles. Finding a rectangle's area and perimeter isn’t tough. This C program gets the rectangle's length and width and calculates both values for you.
This can be super handy in real life. Let’s see how to do it with C.
Prerequisites for Calculating Area and Perimeter in C
Before coding, ensure you know the following:
- Usage of int data types for variables like length and width.
- Basic arithmetic operators:
*for multiplication,+, and*again for the perimeter formula. - Any C compiler such as GCC 9 or newer — or just run it in the EduSeekho online compiler.
Source Code of C Program to Find the Area and Perimeter of a Rectangle
Here's the complete C code that calculates the area and perimeter of a rectangle based on input values.
#include <stdio.h>
int main() {
int length, width, area, perimeter;
// Input length and width
printf("Enter the length of the rectangle: ");
scanf("%d", &length);
printf("Enter the width of the rectangle: ");
scanf("%d", &width);
// Compute area and perimeter
area = length * width;
perimeter = 2 * (length + width);
// Output area and perimeter
printf("Area of the rectangle = %d\n", area);
printf("Perimeter of the rectangle = %d\n", perimeter);
return 0;
}Try this code yourself — free
Run it in the EduSeekho online compiler. No setup needed.
Output of the Area and Perimeter Program
Once you run the program and provide the inputs, it displays the results as shown below.
Enter the length of the rectangle: 5
Enter the width of the rectangle: 3
Area of the rectangle = 15
Perimeter of the rectangle = 16
Algorithm to Calculate Area and Perimeter in C
Calculating a rectangle's area and perimeter involves multiplying its dimensions and using simple arithmetic. Here's the step-by-step logic the program follows:
- Start the program.
- Read the rectangle's length and width from the user.
- Calculate the area as
length * width. - Calculate the perimeter as
2 * (length + width). - Print the area and perimeter.
- End the program.
flowchart TD A([Start]) --> B[/Read length and width/] B --> C[area = length * width] C --> D[perimeter = 2 * (length + width)] D --> E[/Print area and perimeter/] E --> F([End])
How the C Program for Area and Perimeter Works
Let's walk through how the program works line by line:
- Includes the
<stdio.h>library to use theprintfandscanffunctions. - The
mainfunction defines int variables forlength,width,area, andperimeter. - Prompts the user to enter the rectangle's length and stores it in
length. - Prompts for the width and stores it in
width. - Calculates the area using
length * widthand assigns it toarea. - Calculates the perimeter using
2 * (length + width)and assigns it toperimeter. - Prints both the
areaandperimeterwith explanatory strings. - Ends the program with
return 0.
Quick Quiz — test your understanding
Tap an option to check your answerWhat formula is used to calculate the perimeter of a rectangle?
Time and Space Complexity of the C Program
This program's efficiency is straightforward given the computations involved.
Time Complexity: O(1). The program executes a fixed number of operations regardless of input values. It reads two inputs and performs two calculations, so it always runs in the same time.
Space Complexity: O(1). The program uses four int variables: length, width, area, and perimeter. Their count doesn't change with different inputs.
Common mistake: Forgetting to use parentheses in 2 * (length + width) can lead to wrong results for the perimeter.
Key takeaway: Knowing formulas is key for accurate programming, especially in geometry.
FAQs About C Program to Find the Area and Perimeter
Students often have specific questions about calculating area and perimeter. Here are some common ones:
length * width, and the perimeter is 2 * (length + width).Related Programs
If you found this program helpful, check out these related programs:
- C Program to Calculate Simple and Compound Interest
- C Program to Calculate Difference Between Two Time Periods
- C Program to Calculate Area and Circumference of Circle
- C Program to Find the Roots of a Quadratic Equation
- C Program to Calculate Standard Deviation — Full Code & Output
Conclusion
Geometry calculations like these pop up a lot. If you ever need to calculate other shapes, knowing how C handles these basics is super useful. Next time, try working with circles.
Practice more C programs to really nail this down. Check out more exercises in our C quiz or jump back to coding with the C compiler.