Rust program to find the square root of the given number

Given a number, we have to find its square root.
Submitted by Nidhi, on September 28, 2021

Problem Solution:

Here, we will read the number from the user. Then we will calculate the square root of a given number using the sqrt() function and print the result.

Program/Source Code:

The source code to find the square root of the given number is given below. The given program is compiled and executed successfully.

// Rust program to find the 
// square root of given number

use std::io;

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

    
    res = n.sqrt();
    
    println!("Square root is: {}",res);
}

Output:

RUN 1:
Enter number: 
36
Square root is: 6

RUN 2:
Enter number: 
18
Square root is: 4.2426405

RUN 3:
Enter number: 
-1
Square root is: NaN

Explanation:

Here, we read the number from the user. Then we calculated the square root of a given number using the sqrt() function and printed the result.

Rust Basic Programs »



Related Programs




Comments and Discussions!

Load comments ↻






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