Question:
Given the perimeter of a rectangle, the task is to find the maximum area of a rectangle which can be formed.
Example:
Input: perimeter = 15
Output: Maximum Area = 12
Given the perimeter of a rectangle, the task is to find the maximum area of a rectangle which can be formed.
Example:
Input: perimeter = 15
Output: Maximum Area = 12
Solution:
Approach:
For the area to be maximum of any rectangle the difference of length and breadth must be minimal. So, in such case, the length must be ceil (perimeter / 4) and breadth will be floor(perimeter /4).
Hence the maximum area of a rectangle with a given perimeter is equal to ceil(perimeter/4) * floor(perimeter/4).
C program:
#include<stdio.h>
// Function to find max area
float maxArea(float perimeter)
{
int length = (int)ceil(perimeter / 4);
int breadth = (int)floor(perimeter / 4);
// return area
printf("Length=%d \nBreath=%d\n",length,breadth);
return length * breadth;
}
int main()
{
float n;
printf("Enter the Perimeter\n");
scanf("%f",&n);
printf("Maximum Area = %f",maxArea(n));
return 0;
}
Output:
Enter the Perimeter
30
Length=8
Breath=7
Maximum Area = 56.000000
0 comments:
Post a Comment