LearnFrenzy
Sign in Take a free test

Operators and Expressions · Operator Programs · Question 5

Area and perimeter of a rectangle and a circle in C Solution, output & explanation

  • Program
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
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
main.c
#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;
}

Output Verified by compiling and running

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!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links