Home »
Rust »
Rust Programs
Rust program to find the Surface Area and Volume of the Cylinder
Given the radius and height of the cylinder, we have to find the Surface Area and Volume of the Cylinder using Rust program.
Submitted by Nidhi, on September 30, 2021
Problem Solution:
Here, we will read the radius, height from the user. Then we will calculate the surface area and volume of the cylinder and print the result.
Cylinder volume formula: πr2h
Cylinder surface area formula: 2πrh+2πr2
Where r is the radius and h is the height.
Program/Source Code:
The source code to find the Surface Area and volume of the cylinder is given below. The given program is compiled and executed successfully.
// Rust program to find the Surface
// Area and volume of the cylinder
use std::io;
fn main()
{
let mut radius:f32 =0.0;
let mut height:f32 =0.0;
let mut area:f32 =0.0;
let mut volume:f32 =0.0;
let mut input1 = String::new();
let mut input2 = String::new();
println!("Enter radius: ");
io::stdin().read_line(&mut input1).expect("Not a valid string");
radius = input1.trim().parse().expect("Not a valid number");
println!("Enter height: ");
io::stdin().read_line(&mut input2).expect("Not a valid string");
height = input2.trim().parse().expect("Not a valid number");
volume = 3.14 * radius * radius * height;
area = 2.0 * 3.14 * radius * (radius + height);
println!("Volume of Cylinder is: {}", volume);
println!("Surface area of Cylinder is: {}", area);
}
Output:
Enter radius:
5
Enter height:
3.2
Volume of Cylinder is: 251.2
Surface area of Cylinder is: 257.48
Explanation:
Here, we read the radius, height from the user. Then we calculated the surface area and volume of the Cylinder and printed the result.
Rust Basic Programs »