Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
C Program for Area And Perimeter Of Rectangle
Given a length and breadth of a rectangle we have to find its area and perimeter.
Rectangle is a 2-D figure containing four sides and four angles of 90 degrees each. All the sides of rectangle are not equal − only the opposite sides of a rectangle are equal. The diagonals in a rectangle also are of the same length.
Syntax
Area = Length × Breadth Perimeter = 2 × (Length + Breadth)
Algorithm
The formulas for calculating rectangle properties are −
- Area: Length × Breadth
- Perimeter: 2 × (Length + Breadth)
Example: Using Functions
This program demonstrates calculation of area and perimeter using separate functions −
#include <stdio.h>
/* Function to calculate area */
int area(int length, int breadth) {
return length * breadth;
}
/* Function to calculate perimeter */
int perimeter(int length, int breadth) {
return 2 * (length + breadth);
}
int main() {
int length = 20;
int breadth = 30;
printf("Length: %d, Breadth: %d
", length, breadth);
printf("Area of rectangle is: %d
", area(length, breadth));
printf("Perimeter of rectangle is: %d
", perimeter(length, breadth));
return 0;
}
Length: 20, Breadth: 30 Area of rectangle is: 600 Perimeter of rectangle is: 60
Example: Using User Input
This example takes length and breadth from the user −
#include <stdio.h>
int main() {
float length, breadth, area, perimeter;
printf("Enter length of rectangle: ");
scanf("%f", &length);
printf("Enter breadth of rectangle: ");
scanf("%f", &breadth);
area = length * breadth;
perimeter = 2 * (length + breadth);
printf("\nRectangle Properties:
");
printf("Length: %.2f
", length);
printf("Breadth: %.2f
", breadth);
printf("Area: %.2f
", area);
printf("Perimeter: %.2f
", perimeter);
return 0;
}
Key Points
- Rectangle area formula: Length × Breadth
- Rectangle perimeter formula: 2 × (Length + Breadth)
- Use
floatfor decimal precision in measurements - Functions make code modular and reusable
Conclusion
Calculating rectangle area and perimeter in C is straightforward using basic arithmetic operations. Functions improve code organization and allow for easy reuse of calculation logic.
Advertisements
