find the surface area of a regular polygon circumscribed around a triangular prism in rust

To find the surface area of a regular polygon circumscribed around a triangular prism in Rust, we can use the following steps:

  1. Get the height, base length, and number of sides of the polygon from the user.
main.rs
use std::io;

fn main() {
    let mut height = String::new();
    let mut base_length = String::new();
    let mut num_sides = String::new();

    println!("Enter the height of the triangle prism: ");
    io::stdin().read_line(&mut height).expect("Failed to read input.");

    println!("Enter the base length of the triangle prism: ");
    io::stdin().read_line(&mut base_length).expect("Failed to read input.");

    println!("Enter the number of sides of the polygon: ");
    io::stdin().read_line(&mut num_sides).expect("Failed to read input.");

    let height: f64 = height.trim().parse().expect("Invalid input.");
    let base_length: f64 = base_length.trim().parse().expect("Invalid input.");
    let num_sides: i32 = num_sides.trim().parse().expect("Invalid input.");
}
779 chars
21 lines
  1. Calculate the apothem of the polygon using the formula a = b / (2 * tan(π/n)), where b is the base length and n is the number of sides.
main.rs
    let apothem: f64 = base_length / (2.0 * (num_sides as f64).tan() * std::f64::consts::PI / num_sides as f64);
113 chars
2 lines
  1. Calculate the area of one side of the prism by multiplying the base length by the height of the prism.
main.rs
    let side_area: f64 = base_length * height;
47 chars
2 lines
  1. Calculate the surface area of the polygon by multiplying the perimeter of the polygon by the apothem and dividing by 2, and then adding the area of the two triangular faces of the prism.
main.rs
    let polygon_perimeter = base_length * num_sides as f64;
    let polygon_area = polygon_perimeter * apothem / 2.0;
    let triangle_area = 0.5 * base_length * height;
    let surface_area = polygon_area * 2.0 + triangle_area * 2.0;
235 chars
5 lines

Finally, we can print out the surface area of the polygon:

main.rs
    println!("The surface area of the regular polygon circumscribed around the triangular prism is {}.", surface_area);
}
122 chars
3 lines

gistlibby LogSnag