Home »
Rust »
Rust Programs
Rust program to calculate the area of the rhombus
Given the length of the diagonals, we have to calculate the area of the rhombus using Rust program.
Submitted by Nidhi, on September 29, 2021
Problem Solution:
Here, we will read diagonal1, diagonal2 of the rhombus from the user. Then we will calculate the area of the rhombus and print the result.
Area of the rhombus formula = ½ × d1 × d2
Where d1 is the length of diagonal 1 and d2 is the length of diagonal 2.
Program/Source Code:
The source code to calculate the area of the rhombus is given below. The given program is compiled and executed successfully.
// Rust program to calculate the area of rhombus
use std::io;
fn main()
{
let mut diagonal1 :f32 = 0.0;
let mut diagonal2 :f32 = 0.0;
let mut area:f32= 0.0;
let mut input1 = String::new();
let mut input2 = String::new();
println!("Enter diagonal1: ");
io::stdin().read_line(&mut input1).expect("Not a valid string");
diagonal1 = input1.trim().parse().expect("Not a valid number");
println!("Enter diagonal2: ");
io::stdin().read_line(&mut input2).expect("Not a valid string");
diagonal2 = input2.trim().parse().expect("Not a valid number");
area = 0.5 * diagonal1 * diagonal2;
println!("Area of rhombus is: {}", area);
}
Output:
Enter diagonal1:
1.2
Enter diagonal2:
2.3
Area of rhombus is: 1.38
Explanation:
Here, we read the value of diagonal1, diagonal2 from the user. Then we calculated the area of the rhombus and printed the result.
Rust Basic Programs »