Home »
Rust »
Rust Programs
Rust program to calculate the surface area, volume, and space diagonal of cuboids
Given height, width, and length, we have to calculate the surface area, volume, and space diagonal of cuboids using Rust program.
Submitted by Nidhi, on September 30, 2021
Problem Solution:
Here, we will read the height, width, length from the user. Then we will calculate the surface area, volume, and space diagonal of cuboids and print the result.
The surface area of cuboids:
A cuboid has 6 rectangular faces. To find the surface area of a cuboid, add the areas of all 6 faces. Consider that the length is l, width is w, and height is h of the prism and then the formula to calculate the Surface area of cuboids will be: 2lw + 2lh + 2hw
Volume of cuboids:
The formula to calculate the volume of cuboids (V) = whl
Space diagonal of cuboids: √(l2 + b2 +h2)
Program/Source Code:
The source code to calculate the surface area, volume, and space diagonal of cuboids is given below. The given program is compiled and executed successfully.
// Rust program to calculate the surface area,
// volume, and space diagonal of cuboids
use std::io;
fn main()
{
let mut height:f32 =0.0;
let mut width:f32 =0.0;
let mut length:f32 =0.0;
let mut area:f32 =0.0;
let mut volume:f32 =0.0;
let mut diagonal:f32=0.0;
let mut input1 = String::new();
let mut input2 = String::new();
let mut input3 = String::new();
println!("Enter height: ");
io::stdin().read_line(&mut input1).expect("Not a valid string");
height = input1.trim().parse().expect("Not a valid number");
println!("Enter width: ");
io::stdin().read_line(&mut input2).expect("Not a valid string");
width = input2.trim().parse().expect("Not a valid number");
println!("Enter length: ");
io::stdin().read_line(&mut input3).expect("Not a valid string");
length = input3.trim().parse().expect("Not a valid number");
diagonal =(width * width).sqrt() + (length * length) + (height * height);
area = 2.0 * (width * length) + (length * height) + (height * width);
volume = width * length * height;
println!("Volume of Cuboids is : {}", volume);
println!("Surface area of Cuboids is : {}", area);
println!("Space diagonal of Cuboids is: {}", diagonal);
}
Output:
Enter height:
10.2
Enter width:
14.7
Enter length:
16.5
Volume of Cuboids is : 2474.01
Surface area of Cuboids is : 803.34
Space diagonal of Cuboids is: 390.99
Explanation:
Here, we read the height, width, and length from the user. Then we calculated the surface area, volume, and space diagonal and printed the result.
Rust Basic Programs »