✓ Link copied to clipboard
C 📅 June 30, 2026 ⏱ 5 min read

C Program to Find the Area and Perimeter of a Rectangle

C Program to Find the Area and Perimeter of a Rectangle

If you’ve ever measured a room to fit a carpet or frame, you've worked with rectangles. Finding a rectangle's area and perimeter isn’t tough. This C program gets the rectangle's length and width and calculates both values for you.

This can be super handy in real life. Let’s see how to do it with C.

Prerequisites for Calculating Area and Perimeter in C

Before coding, ensure you know the following:

  • Usage of int data types for variables like length and width.
  • Basic arithmetic operators: * for multiplication, +, and * again for the perimeter formula.
  • Any C compiler such as GCC 9 or newer — or just run it in the EduSeekho online compiler.

Source Code of C Program to Find the Area and Perimeter of a Rectangle

Here's the complete C code that calculates the area and perimeter of a rectangle based on input values.

area_perimeter_rectangle.cc▶ Run
#include <stdio.h>

int main() {
    int length, width, area, perimeter;
    // Input length and width
    printf("Enter the length of the rectangle: ");
    scanf("%d", &length);
    printf("Enter the width of the rectangle: ");
    scanf("%d", &width);
    // Compute area and perimeter
    area = length * width;
    perimeter = 2 * (length + width);
    // Output area and perimeter
    printf("Area of the rectangle = %d\n", area);
    printf("Perimeter of the rectangle = %d\n", perimeter);
    return 0;
}

Try this code yourself — free

Run it in the EduSeekho online compiler. No setup needed.

▶ Open Compiler

Output of the Area and Perimeter Program

Once you run the program and provide the inputs, it displays the results as shown below.

OUTPUTtext
Enter the length of the rectangle: 5
Enter the width of the rectangle: 3
Area of the rectangle = 15
Perimeter of the rectangle = 16
Output of C Program to Find the Area and Perimeter of a Rectangle
Output of C Program to Find the Area and Perimeter of a Rectangle

Algorithm to Calculate Area and Perimeter in C

Calculating a rectangle's area and perimeter involves multiplying its dimensions and using simple arithmetic. Here's the step-by-step logic the program follows:

  1. Start the program.
  2. Read the rectangle's length and width from the user.
  3. Calculate the area as length * width.
  4. Calculate the perimeter as 2 * (length + width).
  5. Print the area and perimeter.
  6. End the program.
flowchart TD
  A([Start]) --> B[/Read length and width/]
  B --> C[area = length * width]
  C --> D[perimeter = 2 * (length + width)]
  D --> E[/Print area and perimeter/]
  E --> F([End])
Flowchart: the algorithm step by step

How the C Program for Area and Perimeter Works

Let's walk through how the program works line by line:

  • Includes the <stdio.h> library to use the printf and scanf functions.
  • The main function defines int variables for length, width, area, and perimeter.
  • Prompts the user to enter the rectangle's length and stores it in length.
  • Prompts for the width and stores it in width.
  • Calculates the area using length * width and assigns it to area.
  • Calculates the perimeter using 2 * (length + width) and assigns it to perimeter.
  • Prints both the area and perimeter with explanatory strings.
  • Ends the program with return 0.
🧠

Quick Quiz — test your understanding

Tap an option to check your answer

What formula is used to calculate the perimeter of a rectangle?

🎉Correct! The perimeter is calculated as 2 * (length + width).
🤔Not quite. Check the formula for perimeter again.

Time and Space Complexity of the C Program

This program's efficiency is straightforward given the computations involved.

Time Complexity: O(1). The program executes a fixed number of operations regardless of input values. It reads two inputs and performs two calculations, so it always runs in the same time.

Space Complexity: O(1). The program uses four int variables: length, width, area, and perimeter. Their count doesn't change with different inputs.

⚠️

Common mistake: Forgetting to use parentheses in 2 * (length + width) can lead to wrong results for the perimeter.

Key takeaway: Knowing formulas is key for accurate programming, especially in geometry.

FAQs About C Program to Find the Area and Perimeter

Students often have specific questions about calculating area and perimeter. Here are some common ones:

The area's length * width, and the perimeter is 2 * (length + width).
The program does a fixed number of operations for any input size, so it's O(1).
It uses int variables for length, width, area, and perimeter.
Yeah, just change int to float or double for more precision.
You’ll get wrong results because of operator precedence. Parentheses make sure addition happens first.
Not usually, since area and perimeter need different calculations.

If you found this program helpful, check out these related programs:

Conclusion

Geometry calculations like these pop up a lot. If you ever need to calculate other shapes, knowing how C handles these basics is super useful. Next time, try working with circles.

Practice more C programs to really nail this down. Check out more exercises in our C quiz or jump back to coding with the C compiler.