Area of Rectangle Program in C

Published: 30 Oct 2025 | Reading Time: 6 min read

Overview

This comprehensive tutorial teaches you how to calculate the area of a rectangle using C programming. You'll learn multiple implementation approaches, understand core programming concepts, and build a strong foundation in C fundamentals through practical examples.

What You'll Learn in This Tutorial

Introduction

"The best way to learn programming is to make your logic visible."

Every programmer starts small and often with a rectangle. Calculating the area of a rectangle program in C is one of the simplest yet most practical exercises that introduces beginners to logic, syntax, and computation. It's simple and easy to understand. All you need to do is multiply the length by the width, and you get the area. This formula is not special for mathematics classes only; it is found to be used all over, ranging from surveying land plots to designing buildings.

In this tutorial, you will not only compute the area of a rectangle but also learn how a simple shape can teach you the core building blocks of C: variables, input/output, arithmetic operations, and functions in a fun, practical way.

Understanding the Rectangle

A rectangle is a fundamental shape in geometry, defined as a four-sided flat figure where each angle is a right angle (90 degrees). The opposite sides of a rectangle are equal in length, and the sides are typically referred to as the length and the width (or breadth).

Area Formula

The area of rectangle A = L * B

Where:

Key Properties of a Rectangle

Bottom Line: Understanding these properties is essential before proceeding to arithmetic calculations and programming tasks, such as computing the area or perimeter using variables and input/output operations in C.

Related Programming Concepts in C to Calculate the Area of a Rectangle

When writing programs such as area calculation in C, it's important to understand several basic C programming concepts that make the logic work efficiently. These include functions, input/output operations, and data types, the building blocks of all C programs.

1. Functions

Functions in the C language help to facilitate a large amount of code into a smaller amount of code.

For example, if you wrote your separate function to calculate the area of a circle or rectangle, your program now has much more readability, and you might use the name of the function in multiple places in the source code.

2. Input and Output Operations

The program is now ready to accept user input (the radius or length of the rectangle), and in the event the area is calculated, the output from the program can be displayed on the screen using a standard I/O operation. By using the standard I/O routines (scanf() and printf()), you would substitute in these two I/O routines with a variable to store the radius or length (or whichever is called) and then display to the screen the object we had already calculated.

3. Data Types

In addition to I/O operations, C supports a number of data types, including int, float, and double, which means that the programmer must remember to correctly select the type of data when specifying these and using them in calculations, especially when more than one value is being used in calculations, as the correct type determines whether or not a correct arithmetic ability can be calculated. This is necessary for performing calculations involving decimal values, such as calculating π (pi) or the previously determined area.

4. Arithmetic Operations

Patterns are the most basic operations that are at the core of a C program, which perform calculations. For example, multiplicity is to calculate an area:

area = length * width;

Understanding how to use arithmetic operators is essential for solving mathematical problems in the C language.

In short, understanding C basic programs, functions, data types, and I/O operations helps you write efficient and error-free programs. These concepts are essential for performing arithmetic operations and handling user input in real-world problem-solving using the C language.

Algorithm and Step-by-Step Process for Area of Rectangle Program in C

In programming, an algorithm is a clearly defined series of steps used to logically and efficiently take action to solve a specific problem. For example, when calculating the area of a shape in C programming, an algorithm would picture a roadmap that allows you to implement the programmatic solution in a specified fashion, from input to output. While understanding how to apply the algorithm is vital to the learning process, it will also develop your problem-solving skills that can be applied to many practices in computer science.

Algorithm

  1. Start the program
  2. Display menu or shape (e.g. circle, rectangle, triangle)
  3. Input specified dimensions, such as radius, length, breadth, or base and height
  4. Use the proper application of the formula for the area of a specified shape:
    • Circle → Area = π × r²
    • Rectangle → Area = length × breadth
    • Triangle → Area = ½ × base × height
  5. Display the result of the calculation of the area
  6. End program

This systematic routine ensures accuracy and clarity, which are two main principles of algorithm design. After experiencing this type of simple but practical algorithm, moving forward sacrifices some of those elements to progress towards solving more complex real-world programming and applications.

Code Implementation of Area of Rectangle in C

Now that you are acquainted with the logic, let's put it on paper with code.

In C programming, implementation is where theory meets execution; this is where you translate your logic and formulas into a working solution.

When you write a program to calculate the area of a rectangle, you're not just learning syntax; you are practicing real-world computational thinking:

Let us now discuss three functional ways to begin coding that reinforce your programming foundation in a sequential way:

  1. Using Loops – for controlled execution and comprehension of iteration logic
  2. Using Functions – for modular and reusable code
  3. Using Direct Input (scanf) – for simplicity and fast computation

Each method introduces a different programming mindset, helping you think like a developer, not just a coder.

C Program for Area of Rectangle Using FOR Loop

#include <stdio.h>

int main() {
    float length, breadth, area;

    // Loop to calculate the area of the rectangle once
    for (int i = 0; i < 1; i++) {
        printf("Enter the Length of the Rectangle: ");
        scanf("%f", &length);

        printf("Enter the Breadth of the Rectangle: ");
        scanf("%f", &breadth);

        // Calculate area
        area = length * breadth;

        // Display the result
        printf("The Area of the Rectangle is: %.2f\n", area);
    }

    return 0;
}

Output

Enter the Length of the Rectangle: 30
Enter the Breadth of the Rectangle: 20
The Area of the Rectangle is: 600.00

Code Explanation

Variable Declaration:

For Loop:

User Input:

Area Calculation:

Output:

C Program for Area of Rectangle Using Functions

#include<stdio.h>

// Function to calculate the area of the rectangle
float calculateArea(float length, float breadth) {
    return length * breadth;
}

int main() {
    float length, breadth, area;

    // Input length and breadth from the user
    printf("Enter the Length of the Rectangle: ");
    scanf("%f", &length);

    printf("Enter the Breadth of the Rectangle: ");
    scanf("%f", &breadth);

    // Call the function to calculate the area
    area = calculateArea(length, breadth);

    // Display the result
    printf("The Area of the Rectangle is: %.2f\n", area);

    return 0;
}

Output

Enter the Length of the Rectangle: 5
Enter the Breadth of the Rectangle: 4
The Area of the Rectangle is: 20.00

How the Code Works

Function Declaration and Definition:

Main Function:

Input from User:

Calling the Function:

Output:

C Program for Area of Rectangle Using scanf

#include<stdio.h>

int main()
{
    float length,breadth;
    float area;

    printf(" Enter the Length of a Rectangle : ");
    scanf("%f",&length);

    printf("\n Enter the Breadth of a Rectangle : ");
    scanf("%f",&breadth);

    area = length * breadth;

    printf("\n Area of Rectangle is : %f",area);
    return 0;
}

Output

 Enter the Length of a Rectangle : 6

 Enter the Breadth of a Rectangle : 5

 Area of Rectangle is : 30.000000

=== Code Execution Successful ===

Code Explanation

Variable Declaration:

Input from User:

Area Calculation:

Output:

Therefore, the area is calculated by multiplying the length by the breadth. It gives the result stored in the area variable and prints it as output.

Quick Note: Practicing multiple code approaches to solve the same problem helps to create flexibility, which is needed to solve algorithmic problems given to you as a real-world problem from employers.

Conclusion

Calculating the area of a rectangle program in C may seem so elementary, but it also helps you as a foundation for examining all that you need to become a confident programmer. It helps you master the use of variables, arithmetic operators, and input/output functions, which form the building blocks of every programming concept. Once you're comfortable with these basics, you'll find it easier to understand logic building, debugging, and real-world application design.

Every expert coder once started with simple problems like this. What matters most is consistent practice and curiosity to learn more.

To strengthen these core concepts and move from beginner to professional, consider structured programs like CCBP 4.0 Academy, which help learners build logic, solve real problems, and grow into job-ready developers.

Key Takeaways

Frequently Asked Questions

1. How do I calculate the area of a rectangle in C?

You can calculate the area, simply by multiplying length by width.

area = length * width;

Make sure both variables are declared with suitable data types such as int, float, or double depending on your input precision.

2. How do I take user input for length and width?

You can use the scanf() function to take user input in C:

printf("Enter length and width: ");
scanf("%f %f", &length, &width);

Here, %f is used for floating-point values. Always use the address-of operator (&) when reading input.

3. What is the correct output format for displaying the area?

Use printf() with appropriate format specifiers:

printf("Area of Rectangle = %.2f\n", area);

The %.2f ensures that the output format shows two decimal places.

4. What common errors should I avoid?

5. How can I also find the perimeter?

The formula for the perimeter of a rectangle is:

perimeter = 2 * (length + width);

You can display it using printf() in the same way as the area.

6. What logic should I follow for area calculation?

The logic of the calculation:

  1. Accept user input for length and width
  2. Multiply to find the area
  3. Output the results

7. Can I use integer inputs instead of float?

Yes, if you expect whole numbers then use int instead of float:

int length, width, area;

However, for accuracy, especially in real-world measurements, float or double is preferable.

8. Why is user input important in this program?

User input creates an interactive and dynamic program. Instead of hardcoding values, enabling the user to enter a different length and width each time improves flexibility and usability.

9. Can I calculate both area and perimeter in the same program?

Absolutely. You can combine both calculations as shown below:

#include <stdio.h>

int main() {
    float length, width, area, perimeter;

    printf("Enter length and width: ");
    scanf("%f %f", &length, &width);

    area = length * width;
    perimeter = 2 * (length + width);

    printf("Area = %.2f\n", area);
    printf("Perimeter = %.2f\n", perimeter);

    return 0;
}

Related Articles


About NxtWave: NxtWave offers comprehensive programming education through CCBP 4.0 Academy and other learning programs, helping students build job-ready skills in software development and technology.