ADVERTISEMENT
ADVERTISEMENT

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 »



ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.