ADVERTISEMENT
ADVERTISEMENT

Rust program to demonstrate the use of abs() function

Given a number, we have to find its absolute value using the abs() function.
Submitted by Nidhi, on September 28, 2021

Problem Solution:

Here, we will read the number from the user. Then we will find the absolute value of the number and print the result.

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.

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.