Q:

Rust program to find the square root of the given number

belongs to collection: Rust Basic Programs

0

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.

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 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.

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 demonstrate the use of abs() funct... >>
<< Rust program to calculate the power of a given num...