Published: 30 Oct 2025 | Reading Time: 6 min read
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.
"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.
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).
The area of rectangle A = L * B
Where:
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.
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.
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.
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.
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.
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.
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.
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.
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:
Each method introduces a different programming mindset, helping you think like a developer, not just a coder.
#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;
}
Enter the Length of the Rectangle: 30
Enter the Breadth of the Rectangle: 20
The Area of the Rectangle is: 600.00
Variable Declaration:
float length, breadth, area; - These variables contain the length, breadth, and area of the rectangle.For Loop:
for (int i = 0; i < 1; i++) ensures the block of code runs just once. While this may seem redundant, it shows how a loop structure can be used to perform the calculation.User Input:
scanf().Area Calculation:
area = length * breadth.Output:
printf() in two decimal places.#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;
}
Enter the Length of the Rectangle: 5
Enter the Breadth of the Rectangle: 4
The Area of the Rectangle is: 20.00
Function Declaration and Definition:
float calculateArea(float length, float breadth) takes two parameters, which are length and breadth, both of type float, and will return the area obtained from the multiplication of length times breadth.Main Function:
main() function, we will declare three variables: length, breadth, and area, all of type float.Input from User:
scanf("%f", &length) and scanf("%f", &breadth) are used to capture the user inputs for length and breadth.Calling the Function:
calculateArea(length, breadth), passing it the user-supplied length and breadth values, and returns the calculated area, which is kept in the variable area.Output:
printf("The Area of the Rectangle is: %.2f\n", area);, where %.2f ensures the result is displayed with two decimal places for better formatting.#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;
}
Enter the Length of a Rectangle : 6
Enter the Breadth of a Rectangle : 5
Area of Rectangle is : 30.000000
=== Code Execution Successful ===
Variable Declaration:
float length, breadth - These variables are defined to hold the length and width of the rectangle, as floating-point numbers.float area - The variable float area was defined to hold the result of the area calculation.Input from User:
printf("Enter the Length of a Rectangle: "); used to prompt the user to enter the length of the rectangle.scanf("%f", &length); reads the user's input for the length and stores it in the length variable.printf("\n Enter the Breadth of a Rectangle: "); and stores the input in the breadth variable using scanf("%f", &breadth);.Area Calculation:
area = length * breadth; receives the multiplication of the values of length and breadth to calculate the area of the rectangle and stores the result in the area variable.Output:
printf("\n Area of Rectangle is : %f", area); displays the calculated area to the user.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.
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.
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.
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.
Use printf() with appropriate format specifiers:
printf("Area of Rectangle = %.2f\n", area);
The %.2f ensures that the output format shows two decimal places.
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.
The logic of the calculation:
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.
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.
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;
}
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.