Q:

Rust program to calculate the area of the circle

belongs to collection: Rust Basic Programs

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Rust Basic Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Rust program to calculate the perimeter of the cir... >>
<< Rust program to print the type of variables...