Rust program to calculate the perimeter of the circle

Input radius of the circle, find the perimeter 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 perimeter of the circle.

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.

Rust Basic Programs »



Related Programs




Comments and Discussions!

Load comments ↻






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