Rust program to calculate the area of Trapezium

Given the values of base1, base2, and height, we have to calculate the area of Trapezium using Rust program.
Submitted by Nidhi, on September 29, 2021

Problem Solution:

Here, we will read base1, base2, 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.

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 base1, base2, and height from the user. Then we calculated the area of the Trapezium and printed the result.

Rust Basic Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.