find the surface area of a triangular prism in rust

To find the surface area of a triangular prism, we need to calculate the area of its five faces: two triangular bases and three rectangular lateral faces.

Here's the Rust code to calculate the surface area of a triangular prism:

main.rs
fn surface_area_of_triangular_prism(base_a: f32, base_b: f32, height: f32, prism_height: f32) -> f32 {
    let base_area = 0.5 * base_a * height + 0.5 * base_b * height;  // area of the two triangular bases
    let lateral_area = 2.0 * height * prism_height + base_a * prism_height + base_b * prism_height;  // area of three rectangular lateral faces
    let total_area = base_area + lateral_area;  // total surface area is the sum of base and lateral areas
    total_area
}

// Example usage
fn main() {
    let base_a = 4.0;
    let base_b = 6.0;
    let height = 3.0;
    let prism_height = 5.0;
    let surface_area = surface_area_of_triangular_prism(base_a, base_b, height, prism_height);
    println!("Surface Area of Triangular Prism: {}", surface_area);
}
764 chars
17 lines

In this program, we define a function surface_area_of_triangular_prism that calculates the surface area using the input values for the base and height of the triangular faces and the height of the prism. We then use this function to calculate the surface area of a triangular prism with base side lengths of 4 and 6, height of 3, and a prism height of 5.

The output of the program is:

main.rs
Surface Area of Triangular Prism: 97
37 chars
2 lines

gistlibby LogSnag