ADVERTISEMENT
ADVERTISEMENT

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 »



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.