Q:

Rust program to calculate the area of Trapezium

belongs to collection: Rust Basic Programs

0

Here, we will read base1base2, the height of Trapezium from the user. Then we will calculate the area of the Trapezium and print the result.

Area of Trapezium formula = h/2(b1+b2)

Where h is the height, b1 is the base1, and b2 is the base2.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program/Source Code:

The source code to calculate the area of Trapezium is given below. The given program is compiled and executed successfully.

// Rust program to calculate the 
// area of Trapezium

use std::io;

fn main() 
{
    let mut base1:f32 = 0.0;
    let mut base2:f32 = 0.0;
    let mut height:f32 = 0.0;
    let mut area:f32 = 0.0;
    
    let mut input1 = String::new();
    let mut input2 = String::new();
    let mut input3 = String::new();
       
    println!("Enter base1: ");
    io::stdin().read_line(&mut input1).expect("Not a valid string");
    base1 = input1.trim().parse().expect("Not a valid number");
    
    println!("Enter base2: ");
    io::stdin().read_line(&mut input2).expect("Not a valid string");
    base2 = input2.trim().parse().expect("Not a valid number");

    println!("Enter height: ");
    io::stdin().read_line(&mut input3).expect("Not a valid string");
    height = input3.trim().parse().expect("Not a valid number");
    
    area = 0.5 * (base1 + base2) * height;
    
    println!("Area of Trapezium is: {}", area);
}

Output:

Enter base1: 
3
Enter base2: 
4
Enter height: 
1
Area of Trapezium is: 3.5

Explanation:

Here, we read the value of base1base2, and height from the user. Then we calculated the area of the Trapezium and printed the result.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Rust Basic Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Rust program to calculate the area of Parallelogra... >>
<< Rust program to calculate the area of a triangle f...