C Program to Find Area of Rectangle using Function | Structure
C program to find area of rectangle using function and structure.
In this article, you will learn how to make a C program to find area of rectangle using function and structure.
Example
Enter the length & width of the rectangle::
5
7
Area of the rectangle is: 35.000000 units
You should have knowledge of the following topics in c programming to understand this program:
- C
main()
function - C
printf()
function - C Function
- C Structure
Formula of Area of Rectangle
A = l * w
Where
A = Area of the rectangle
l = Length of the rectangle
w = Width of the rectangle
In this article, we solve this problem in three methods:
Source Code
// C Program to Find Area of Rectangle
#include <stdio.h>
int main() {
float length, width, area;
printf("Enter the length & width of the rectangle::\n");
scanf("%f", &length);
scanf("%f", &width);
// It will calculate area of rectangle
area = length * width;
// It will print the final output
printf("\nArea of the rectangle is: %f units\n", area);
return 0;
}
Output
Enter the length & width of the rectangle::
5
7
Area of the rectangle is: 35.000000 units
Explanation
In this given program, we have taken two inputs 5
& 7
length and width of the rectangle from the user via the system console. Then we applied the standard formula to these values to calculate the area.
Then it will return 35.000000
units final output of the above program.
C Program to Find Area of Rectangle using Function
// C Program to Find Area of Rectangle using Function
#include <stdio.h>
void FindArea(float x, float y) {
// It will calculate area of rectangle
float area = x * y;
// It will print the final output
printf("\nArea of the rectangle is: %f units\n", area);
}
int main() {
float length, width;
printf("Enter the length & width of the rectangle::\n");
scanf("%f", &length);
scanf("%f", &width);
FindArea(length, width);
return 0;
}
C Program to Find Area of Rectangle using Structure
// C Program to Find Area of Rectangle using Structure
#include <stdio.h>
struct Rectangle {
float length;
float width;
float area;
};
int main() {
// It will declare `Rect` of type `Rectangle`
struct Rectangle Rect;
printf("Enter the length & width of the rectangle::\n");
scanf("%f", &Rect.length);
scanf("%f", &Rect.width);
// It will calculate area of rectangle
Rect.area = Rect.length * Rect.width;
// It will print the final output
printf("\nArea of the rectangle is: %f units\n", Rect.area);
return 0;
}
Output
Enter the length & width of the rectangle::
45
90
Area of the rectangle is: 4050.000000 units
Also, visit these links
Half Pyramid in C Programming of Stars
Half Pyramid Pattern in C++ language of Stars