Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors

C program to calculate perimeter and area of a rectangle

C
#include <stdio.h>

int main() {
    double length, breadth, perimeter, area;

    printf("Enter the length of the rectangle: ");
    scanf("%lf", &length);

    printf("Enter the breadth of the rectangle: ");
    scanf("%lf", &breadth);

    perimeter = 2 * (length + breadth);
    area = length * breadth;

    printf("Perimeter of the rectangle: %.2lf\n", perimeter);
    printf("Area of the rectangle: %.2lf\n", area);

    return 0;
}

In this program, we declare variables for the length, breadth, perimeter, and area of the rectangle. We prompt the user to enter the length and breadth values using printf and scanf.

Next, we calculate the perimeter by adding the lengths of all four sides, which is 2 * (length + breadth). The area is calculated by multiplying the length and breadth values, which is length * breadth.

Finally, we print the calculated perimeter and area using printf with the format specifier %.2lf to display the values with two decimal places.

Please note that %lf is used for double data types in scanf and printf functions to correctly read and display decimal values.

Output
Enter the length of the rectangle: 5
Enter the breadth of the rectangle: 2
Perimeter of the rectangle: 14.00
Area of the rectangle: 10.00