0
0
CProgramBeginner · 2 min read

C Program to Find Area of Rectangle

To find the area of a rectangle in C, use area = length * width; where length and width are the rectangle's sides.
📋

Examples

Inputlength = 5, width = 3
OutputArea of rectangle: 15
Inputlength = 10, width = 7
OutputArea of rectangle: 70
Inputlength = 0, width = 4
OutputArea of rectangle: 0
🧠

How to Think About It

To find the area of a rectangle, multiply its length by its width. This is like finding how many square units fit inside the shape by counting rows and columns.
📐

Algorithm

1
Get the length of the rectangle from the user
2
Get the width of the rectangle from the user
3
Multiply length by width to calculate the area
4
Display the calculated area
💻

Code

c
#include <stdio.h>

int main() {
    int length, width, area;
    printf("Enter length: ");
    scanf("%d", &length);
    printf("Enter width: ");
    scanf("%d", &width);
    area = length * width;
    printf("Area of rectangle: %d\n", area);
    return 0;
}
Output
Enter length: 5 Enter width: 3 Area of rectangle: 15
🔍

Dry Run

Let's trace the example where length = 5 and width = 3 through the code

1

Input length

User enters 5, so length = 5

2

Input width

User enters 3, so width = 3

3

Calculate area

area = length * width = 5 * 3 = 15

4

Print area

Output displayed: Area of rectangle: 15

StepVariableValue
1length5
2width3
3area15
💡

Why This Works

Step 1: Getting inputs

We ask the user to enter the length and width using scanf to store values in variables.

Step 2: Calculating area

We multiply length and width using * operator to find the area.

Step 3: Displaying result

We print the area using printf so the user sees the answer.

🔄

Alternative Approaches

Using float for decimal inputs
c
#include <stdio.h>

int main() {
    float length, width, area;
    printf("Enter length: ");
    scanf("%f", &length);
    printf("Enter width: ");
    scanf("%f", &width);
    area = length * width;
    printf("Area of rectangle: %.2f\n", area);
    return 0;
}
This allows decimal values for length and width, useful for more precise measurements.
Using functions to calculate area
c
#include <stdio.h>

float calculateArea(int l, int w) {
    return l * w;
}

int main() {
    int length, width;
    printf("Enter length: ");
    scanf("%d", &length);
    printf("Enter width: ");
    scanf("%d", &width);
    float area = calculateArea(length, width);
    printf("Area of rectangle: %.0f\n", area);
    return 0;
}
Separates calculation into a function for cleaner code and reusability.

Complexity: O(1) time, O(1) space

Time Complexity

The program performs a fixed number of operations regardless of input size, so it runs in constant time O(1).

Space Complexity

Only a few variables are used to store inputs and output, so space complexity is O(1).

Which Approach is Fastest?

All approaches run in constant time; using functions adds clarity but no significant speed difference.

ApproachTimeSpaceBest For
Basic integer multiplicationO(1)O(1)Simple integer inputs
Float inputs for decimalsO(1)O(1)Precise measurements with decimals
Function-based calculationO(1)O(1)Code clarity and reuse
💡
Always check that length and width are positive numbers before calculating area.
⚠️
Beginners often forget to use the address operator & in scanf, causing input errors.