C Program to Find Surface Area and Volume of a Rectangular Prism
Introduction
A rectangular prism is a three-dimensional geometric figure with six rectangular faces, also known as a cuboid. It is a common shape in real-world applications, such as boxes, rooms, or storage containers. In this program, we will calculate its surface area and volume using a C programming approach.
Formulas
To calculate the surface area and volume of a rectangular prism, the following formulas are used:
- Surface Area: \( 2 \times (lw + lh + wh) \)
- Volume: \( l \times w \times h \)
Here:
lrepresents the length of the rectangular prism.wrepresents the width.hrepresents the height.
Code
#include <stdio.h>
int main() {
float length, width, height, surface_area, volume;
// Input dimensions
printf("Enter the length of the rectangular prism: ");
scanf("%f", &length);
printf("Enter the width of the rectangular prism: ");
scanf("%f", &width);
printf("Enter the height of the rectangular prism: ");
scanf("%f", &height);
// Calculations
surface_area = 2 * (length * width + length * height + width * height);
volume = length * width * height;
// Output results
printf("The surface area of the rectangular prism is: %.2f\n", surface_area);
printf("The volume of the rectangular prism is: %.2f\n", volume);
return 0;
}
Output
When you run the program and provide the required inputs, the output might look like this:
Enter the length of the rectangular prism: 5
Enter the width of the rectangular prism: 3
Enter the height of the rectangular prism: 4
The surface area of the rectangular prism is: 94.00
The volume of the rectangular prism is: 60.00
Explanation
This program is designed to compute both the surface area and volume of a rectangular prism. Let's break down the process:
- Input: The program first asks the user to input the dimensions of the prism: length, width, and height.
- Calculations:
- The surface area is calculated using the formula \( 2 \times (lw + lh + wh) \). This adds the area of all six faces of the prism.
- The volume is computed by multiplying the three dimensions: \( l \times w \times h \).
- Output: The results are displayed with two decimal points of precision for clarity and accuracy.