Calculating the area of a rectangle is one of the first geometric lessons that any student is taught when it comes to areas chapter. 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.
Now, when you start with programming, the computation of a rectangle is among the best ways to practice. There is a small project useful for newcomers: it provides you with an opportunity to learn the primary programming concepts, including variables, inputs/outputs, and arithmetic operations in C.
The area of rectangle A = L * B
Where L is the length of the longer side
B is the breadth or the shorter side
Algorithm for Area of Rectangle program in C
The algorithm is as follows:
- Begin by declaring two variables to store the length and width of the rectangle. These will hold the user-provided values.
- Prompt the user to enter the length and width of the rectangle, providing clear instructions for input.
- Read the values entered by the user from the console, ensuring that both the length and width are captured correctly.
- Next, calculate the area of the rectangle by multiplying the length and width values together.
- Finally, the calculated area is displayed as the output, showing the result to the user in a clear format.
Code Implementation of Area of Rectangle in C
Now let’s take a look at how to write the program for the area of a rectangle in different ways:
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 Execution Successful ===
How the code works:
Variable Declaration:
- float length, breadth, area; - These variables store the length, breadth, and the calculated area of the rectangle.
For Loop:
- The 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:
- The program prompts the user to input the length and breadth of the rectangle using scanf().
Area Calculation:
- The area is calculated with area = length * breadth;.
Output:
- The calculated area is displayed using printf() and formatted in two dec; al places.
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
=== Code Execution Successful ===
How the code works:
Function Declaration and Definition:
- float calculateArea(float length, float breadth) defines a function that takes two arguments, length and breadth, both of type float. It returns the calculated area by multiplying the length and breadth together.
Main Function:
- In the main() function, we declare three variables: length, breadth, and area, all of type float.
Input from User:
- The program first prompts the user to enter the length and breadth of the rectangle.
- scanf("%f", &length) and scanf("%f", &breadth) are used to capture the user inputs for length and breadth.
Calling the Function:
- The function calculateArea(length, breadth) is called with the user-provided values for length and breadth. The returned value (calculated area) is stored in the variable area.
Output:
- Finally, the program displays the calculated area using printf("The Area of the Rectangle is: %.2f\n", area);, where %.2f ensures the result is displayed with two decimal places for better formatting.
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 ===
Let’s take a look at how the code calculates the area of a rectangle:
- Variable Declaration:
- float length, and breadth - These variables are declared to store the length and breadth of the rectangle as floating-point numbers.
- float area - This variable is declared to store the result of the area calculation.
- Input from User:
- printf("Enter the Length of a Rectangle : "); prompts 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.
- Similarly, the program prompts the user for the breadth with 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; multiplies the length and breadth values 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 and breadth. It gives the result stored in the area variable and printed as output.
Conclusion
Avoiding thinking of writing a program to calculate the area of the rectangle is merely a simple exercise. One can gain or improve some of the basic fundamentals of programming, that is, using variable loops using input/output functions and arithmetic operators. Efficiency in this basic routine is important for individuals who want to lay the foundation for their software development skills. These basic concepts will form some foundational knowledge for approaching future problems and creating applications later on. To build a strong foundation, start early and enroll to the CCBP Academy 4.0 program.
Boost Your Placement Chances by Learning Industry-Relevant Skills While in College!
Explore ProgramFrequently Asked Questions
1. What is the formula for finding the area of the rectangle?
The formula to calculate the area of a rectangle is: Area = Length × Breadth.
2. In C programming, how do you declare the length and breadth?
In C, one may use the int or float data type when declaring variables for length and width – when using a whole number or a decimal value, respectively.
3. Is length and width always positive or can they be negative?
The measure of length and width of a rectangle cannot be negative. It should also be noted that, in practice, negative values are inadmissible for these dimensions. When developing your program, you should check if the input is positive or not.
4. What happens when the input in a C program is invalid?
Dealing with the invalid input, we can use conditional statements taking the neIn dealing valued checking whether they are valid or not.
5. Commby on mistakes while writing this program?
Some of the mistakes people make are using wrong format specifiers, missing the & symbol in scanf, and general failure to check the user inputs. Moreover, mixing integer and floating-point operations can yield different results.