ADVERTISEMENT
ADVERTISEMENT

Rust program to calculate the area of a triangle for a given base and height

Given base and height of a triangle, we have to calculate the area of a triangle using Rust program.
Submitted by Nidhi, on September 29, 2021

Problem Solution:

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

Program/Source Code:

The source code to calculate the area of a triangle for a given base and height is given below. The given program is compiled and executed successfully.

// Rust program to calculate the area of a 
// triangle for a given base and height

use std::io;

fn main() 
{
    let mut height:f32 = 0.0;
    let mut base:f32 = 0.0;
    let mut area:f32= 0.0;
    
    let mut input1 = String::new();
    let mut input2 = String::new();
    
    println!("Enter base: ");
    io::stdin().read_line(&mut input1).expect("Not a valid string");
    base = 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");
    
    area=(base * height) / 2.0;
    
    println!("Area of a triangle: {}", area);
}

Output:

Enter base: 
12
Enter height: 
7
Area of a triangle: 42

Explanation:

Here, we read the base and height from the user. Then we calculated the area of the triangle and printed the result.

Rust Basic Programs »



ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.