Rust program to calculate the area of the circle

Input radius of the circle, find the area of the circle using Rust program.
Submitted by Nidhi, on September 26, 2021

Problem Solution:

Here, we will read the radius from the user and calculate the area of the circle.

Program/Source Code:

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

// Rust program to calculate the area of circle

use std::io;

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

    //Caculate the area of circle
    area = 3.14 * radius * radius;
 
    println!("Area of circle: {}",area);
}

Output:

RUN 1:
Enter radius: 
1.2
Area of circle: 4.5216007

RUN 2:
Enter radius: 
5.15
Area of circle: 83.28066

RUN 3:
Enter radius: 
10
Area of circle: 314

Explanation:

Here, we read the value of radius from the user and calculated the area of the circle. After that, we printed the result.

Rust Basic Programs »



Related Programs



Comments and Discussions!

Load comments ↻





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