Q:

Rust program to demonstrate the use of abs() function

belongs to collection: Rust Basic Programs

0

Here, we will read the number from the user. Then we will find the absolute value of the number 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 demonstrate the use of abs() function is given below. The given program is compiled and executed successfully.

// Rust program to demonstrate the 
// use of abs() function

fn main() {
    let mut n1:i32 = -5;
    let mut n2:i32 = 7;
    let mut res:i32 = 0;
    
    res = n1.abs();
    println!("Absolute value is n1: {}",res);
    
    res = n2.abs();
    println!("Absolute value is n2: {}",res);
}

Output:

Absolute value is n1: 5
Absolute value is n2: 7

Explanation:

Here, we created two integer variables n1 and n2. Then we found the absolute value of n1 and n2 using the abs() function. 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 multiply two numbers using \'... >>
<< Rust program to find the square root of the given ...