Q:

Rust program to calculate the perimeter of the circle

belongs to collection: Rust Basic Programs

0

Here, we will read the radius from the user and calculate the perimeter 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 perimeter of the circle is given below. The given program is compiled and executed successfully.

// Rust program to calculate the 
// perimeter of circle

use std::io;

fn main() {
    let mut radius:f32 = 0.0;
    let mut perimeter: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");

    perimeter = 2.0 * 3.14 * radius;
 
    println!("Perimeter of circle: {}",perimeter);
}

Output:

RUN 1:
Enter radius: 
1.2
Perimeter of circle: 7.5360007

RUN 2:
Enter radius: 
5.15
Perimeter of circle: 32.342003

RUN 3:
Enter radius: 
10
Perimeter of circle: 62.800003

Explanation:

Here, we read the value of radius from the user and calculated the perimeter 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 area of the rectangl... >>
<< Rust program to calculate the area of the circle...