Question 5
Back to Exercise
Compute the area and perimeter of a 7 × 3 rectangle and the area and circumference of a circle of radius 2.
Solution
#include <math.h>
#include <stdio.h>
int main(void) {
int length = 7, width = 3;
printf("rectangle: area %d, perimeter %d\n", length * width, 2 * (length + width));
const double pi = acos(-1.0);
double r = 2;
printf("circle: area %.2f, circumference %.2f\n", pi * r * r, 2 * pi * r);
return 0;
}
rectangle: area 21, perimeter 20 circle: area 12.57, circumference 12.57
The brackets in 2 * (length + width) matter: without them the result would be 2 × 7 + 3 = 17. For r = 2, πr² and 2πr happen to be equal (4π).
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!