C Program for Area and Perimeter of a Rectangle

C Program for Area and Perimeter of a Rectangle

A school exercise asks you to rectangle area and perimeter. Use small, named values first so you can distinguish the required calculation from the syntax used to express it.

Rectangle area multiplies length by width; perimeter adds all four side lengths.

Before this exercise, review operators and conversions and decisions and conditions. The worked program below applies those ideas to this specific problem.

  1. Explain

    Explain why rectangle area and perimeter uses this sequence of operations.

  2. Trace

    Trace the sample values and predict the printed result.

  3. Apply

    Use a garden six units long and four wide.

Rectangle Area and Perimeter: follow the data

  1. Given data

    A school exercise asks you to rectangle area and perimeter. Use small, named values first so you can distinguish the required calculation from the syntax used to express it.

  2. Rule

    Rectangle area multiplies length by width; perimeter adds all four side lengths.

  3. Observed result

    Area: 40; perimeter: 26

A guided model of this example. The illustration explains the process; run the complete C program to check its output.

Understand the operation

A school garden has two independent dimensions. Area counts square units; perimeter measures the total boundary in ordinary length units.

Read the declarations and operands before the output call. %d formats an int; floating-point results use an appropriate precision with %f. A format describes a value that was already computed—it does not choose the earlier arithmetic.

Read the complete C program

The input values are written into this complete C17 example so the calculation can be reproduced without an unavailable service. Change the named data, rebuild and compare the actual output with your prediction.

C17 · complete program

#include <stdio.h>
int main(void) {
  int length = 8, width = 5;
  printf("Area: %d; perimeter: %d\n", length * width, 2 * (length + width));
  return 0;
}

Expected output

Area: 40; perimeter: 26
Run in C compiler →

Follow the values through the code

C17 · code used in this explanation

#include <stdio.h>
int main(void) {
  int length = 8, width = 5;
  printf("Area: %d; perimeter: %d\n", length * width, 2 * (length + width));
  return 0;
}

8 × 5 gives area 40.

2 × (8 + 5) gives perimeter 26.

Follow the worked calculation

  1. Start with the sample

    8 × 5 gives area 40.

  2. Apply the rule

    2 × (8 + 5) gives perimeter 26.

  3. Check the result

    Area: 40; perimeter: 26

    Area: 40; perimeter: 26
A guided model of this example. The illustration explains the process; run the complete C program to check its output.

Try a small variation

C17 · code used in this explanation

#include <stdio.h>
int main(void) {
  int length = 8, width = 5;
  printf("Area: %d; perimeter: %d\n", length * width, 2 * (length + width));
  return 0;
}

Use a garden six units long and four wide.

Worked answer

The worked answer follows the same rule with the requested changed data. Compare its complete source and output: Area: 24; perimeter: 20. The result should follow from the operations, rather than from memorizing the first sample.

C17 · complete program

#include <stdio.h>
int main(void) {
  int length = 6, width = 4;
  printf("Area: %d; perimeter: %d\n", length * width, 2 * (length + width));
  return 0;
}

Expected output

Area: 24; perimeter: 20
Run in C compiler →

Recognize the important boundaries

Reject negative dimensions for an ordinary rectangle. Include units when interpreting the result, and ensure products fit their type.

The demonstrated values stay within the types’ representable ranges. A program accepting arbitrary external values needs checked conversion and bounds before performing arithmetic.

Check your understanding

Choose the answer, then compare the reasoning for every option.

Why are parentheses important in 2 * (length + width)?
Choose one answer. You can try again.

Answer and explanation

Both dimensions must be added before doubling.

Correct. Without grouping, multiplication would affect only the first term.

Use the idea independently

Rectangle area multiplies length by width; perimeter adds all four side lengths.

Continue with arithmetic operand types.

Continue with validated input.

Language reference: WG14 C11 committee draft. These examples use the C17-compatible subset of the explained language features.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: C Program to Check Even or Odd →