Learning how to calculate the area of a rectangle is one of the first and simplest exercises for anyone starting with C programming. It’s a practical example that introduces you to variables, mathematical expressions, and user input—all essential building blocks of coding. With this concept, you not only learn to handle numbers but also understand how computers perform real-world calculations based on formulas.

with hands-on learning.
get the skills and confidence to land your next move.
The formula for calculating the area of a rectangle is straightforward:
Area = Length × Breadth (or Area = Width × Height, depending on your terms).
This calculation is widely used in engineering, architecture, and even simple applications like measuring a plot of land, a room, or a display screen. In this article, we’ll explore different C programs to calculate the area of a rectangle in a clear, step-by-step manner. Each version will help you understand how small changes in code make your program more dynamic and interactive.
Program 1: Calculate Area of Rectangle Using Predefined Values
We’ll begin with the simplest approach — using predefined values for the rectangle’s length and breadth. This example helps you understand the basic formula and how mathematical operations work in C.
#include <stdio.h>
int main() {
float length = 10.5;
float breadth = 6.2;
float area;
area = length * breadth;
printf("Length: %.2f\n", length);
printf("Breadth: %.2f\n", breadth);
printf("Area of Rectangle: %.2f\n", area);
return 0;
}
In this program, both the length and breadth values are predefined. The program multiplies these two values to find the area. Using the printf()
function, it then displays the result neatly formatted to two decimal places. This type of program is ideal for beginners who are just starting with C and want to see how mathematical expressions can be applied directly in code. Even though it uses fixed values, it’s a great first step before moving to interactive programs.
Program 2: Calculate Area of Rectangle Using User Input
To make the program more interactive, let’s allow the user to enter the length and breadth during runtime. This approach is closer to how real applications work.
#include <stdio.h>
int main() {
float length, breadth, area;
printf("Enter the length of the rectangle: ");
scanf("%f", &length);
printf("Enter the breadth of the rectangle: ");
scanf("%f", &breadth);
area = length * breadth;
printf("The area of the rectangle is: %.2f\n", area);
return 0;
}
This program uses the scanf()
function to accept input from the user. Once the user provides the length and breadth, the program multiplies them to calculate the area. This version teaches beginners how to handle input and output in C. It also gives a sense of real-world interaction, where values aren’t fixed but can change each time you run the program.
Program 3: Calculate Area of Rectangle Using Functions
To make the code reusable and cleaner, we can write a function that calculates the area. This is a good example of modular programming in C, which helps you organize code more efficiently.
#include <stdio.h>
float calculateArea(float length, float breadth) {
return length * breadth;
}
int main() {
float length, breadth, area;
printf("Enter the length: ");
scanf("%f", &length);
printf("Enter the breadth: ");
scanf("%f", &breadth);
area = calculateArea(length, breadth);
printf("Area of Rectangle: %.2f\n", area);
return 0;
}
In this program, a separate function called calculateArea()
handles the formula for finding the area. The main function simply collects input from the user and passes it to the function. This approach makes the code cleaner and easier to maintain, especially if you plan to calculate areas for multiple shapes later on. It also introduces the concept of functions, which are reusable blocks of code—a fundamental part of C programming.
Program 4: Calculate Area of Multiple Rectangles
Sometimes, you may want to calculate the area of several rectangles at once—for example, when measuring different rooms in a house or plots of land. The following program shows how to do that using a loop.
#include <stdio.h>
int main() {
int n, i;
float length, breadth, area;
printf("Enter the number of rectangles: ");
scanf("%d", &n);
for(i = 1; i <= n; i++) {
printf("\nEnter length of rectangle %d: ", i);
scanf("%f", &length);
printf("Enter breadth of rectangle %d: ", i);
scanf("%f", &breadth);
area = length * breadth;
printf("Area of rectangle %d: %.2f\n", i, area);
}
return 0;
}
This version uses a for
loop to repeat the process for multiple rectangles. It prompts the user for the dimensions of each rectangle, calculates the area, and displays the result. For beginners, this example is an excellent way to understand loops and repetition in C while still working with simple arithmetic. It demonstrates how the same logic can be applied multiple times to handle multiple inputs.
Program 5: Calculate Area Using Structures
To take it one step further, let’s use structures to represent a rectangle. Structures are a powerful feature in C that allow you to group related variables together.
#include <stdio.h>
struct Rectangle {
float length;
float breadth;
};
int main() {
struct Rectangle rect;
float area;
printf("Enter the length of the rectangle: ");
scanf("%f", &rect.length);
printf("Enter the breadth of the rectangle: ");
scanf("%f", &rect.breadth);
area = rect.length * rect.breadth;
printf("Area of Rectangle: %.2f\n", area);
return 0;
}
In this example, we define a structure named Rectangle
that stores two values—length and breadth. This approach makes the program more organized, especially when dealing with multiple attributes of an object. Beginners can learn how structures make code more readable and flexible, a concept that’s vital in larger projects.
Frequently Asked Questions (FAQ)
Many beginners have similar questions when learning about calculating the area of a rectangle in C. Here are some clear answers to help you understand better.
Q1. What is the formula used to calculate the area of a rectangle?
The formula is Area = Length × Breadth. It’s one of the simplest and most widely used geometric formulas.
Q2. What data type should I use for the variables?
Use float
or double
to handle decimal values accurately, especially when the sides of the rectangle are not whole numbers.
Q3. Can I calculate the perimeter as well in the same program?
Yes, you can easily add another formula: Perimeter = 2 × (Length + Breadth). It can be included in the same program to get both results.
Q4. Why should I use functions or structures in such a simple program?
Functions and structures help make your code modular and organized, preparing you for larger programs where similar logic is reused.
Q5. What happens if I enter negative values?
In real-world scenarios, negative lengths or breadths don’t make sense, so you should always validate input to avoid incorrect calculations.
Conclusion
Calculating the area of a rectangle in C is a fundamental exercise that helps beginners understand how variables, input, and formulas work together in programming. From using simple predefined values to applying functions and structures, each approach builds your skills step by step. By practicing these programs, you’ll gain confidence in writing efficient, readable, and interactive C programs. The best way to learn is by experimenting—try modifying the code, adding features, or creating programs for other shapes like triangles or circles.
Additional & References
To deepen your understanding of C programming and strengthen your problem-solving skills, explore the following resources. They’ll guide you from beginner to intermediate level with practice and examples.
- C Standard Library (stdio.h) – Official reference explaining functions like
printf()
andscanf()
. Excellent for understanding input/output handling. - Programiz C Language Tutorials – Easy-to-follow lessons for beginners, with plenty of examples to practice coding basics like this one.
- GeeksforGeeks C Programming Section – A detailed collection of C programs and explanations that help reinforce core concepts.
- Learn-C.org – A free interactive site that lets you test C programs instantly, perfect for hands-on learning and experimentation.