Write a C Program to Calculate the Area and Perimeter of a Rectangle.
Here's a C program for Area And Perimeter Of Rectangle:
#include<stdio.h>
#include<conio.h>
void main()
{
float length, width, area, perimeter;
// Input the length and width of the rectangle
printf("Enter the length of the rectangle: ");
scanf("%f", &length);
printf("Enter the width of the rectangle: ");
scanf("%f", &width);
// Calculate the area of the rectangle
area = length * width;
// Calculate the perimeter of the rectangle
perimeter = 2 * (length + width);
// Display the calculated results
printf("Area of the rectangle: %.2f square units\n",area);
printf("Perimeter of the rectangle: %.2f units\n",perimeter);
}
#include<conio.h>
void main()
{
float length, width, area, perimeter;
// Input the length and width of the rectangle
printf("Enter the length of the rectangle: ");
scanf("%f", &length);
printf("Enter the width of the rectangle: ");
scanf("%f", &width);
// Calculate the area of the rectangle
area = length * width;
// Calculate the perimeter of the rectangle
perimeter = 2 * (length + width);
// Display the calculated results
printf("Area of the rectangle: %.2f square units\n",area);
printf("Perimeter of the rectangle: %.2f units\n",perimeter);
}
- We first declare four variables are declared: length, width, area, and perimeter. These variables will store the user-input values for length and width, as well as the calculated area and perimeter.
- The program prompts the user to input the length and width of the rectangle using the printf function followed by the scanf function for each input.
- The area of the rectangle is calculated by multiplying the length and width variables.
- The perimeter of the rectangle is calculated using the formula: 2 * (length + width).
- The program then uses the printf function to display the calculated area and perimeter with two decimal places.
Output: