Rust program to calculate the area of the rectangle

Input the length and breadth, find the area of the rectangle using Rust program.
Submitted by Nidhi, on September 26, 2021

Problem Solution:

Here, we will read the value of length and breadth from the user and calculate the area of the rectangle.

Program/Source Code:

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

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

use std::io;

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

    println!("Enter breadth: ");
    io::stdin().read_line(&mut input2).expect("Not a valid string");
    breadth = input2.trim().parse().expect("Not a valid number");

    area = length * breadth;
 
    println!("Area of rectangle: {}",area);
}

Output:

RUN 1:
Enter length: 
5.7
Enter breadth: 
3.4
Area of rectangle: 19.38

RUN 2:
Enter length: 
10
Enter breadth: 
20
Area of rectangle: 200

RUN 3:
Enter length: 
10.5
Enter breadth: 
5.10
Area of rectangle: 53.55

Explanation:

Here, we read the value of length and breadth from the user and calculated the area of the rectangle. After that, we printed the result.

Rust Basic Programs »



Related Programs



Comments and Discussions!

Load comments ↻





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